Property Mappings using Fluent API:

Here, we will learn how to configure properties of an entity class using Fluent API.

We will use the following Student and Standard domain classes of our school application.

public class Student
{
    public Student()
    { 
        
    }
    public int StudentKey { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Standard Standard { get; set; }
}
    
public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardKey { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
   
    }
        

Configure Primary Key and Composite Primary key:

Our domain classes above, do not follow the Code-First convention for primary key because they don't have Id or {Class Name} + Id property. So, you can configure a key property using HasKey() method of EntityTypeConfiguration using Fluent API as below. Remember modelBuilder.Entity<TEntity>() returns EntityTypeConfiguration object.

public class SchoolContext: DbContext 
{
    public SchoolDBContext(): base() 
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Configure primary key
        modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
        modelBuilder.Entity<Standard>().HasKey<int>(s => s.StandardKey);

        //Configure composite primary key
        modelBuilder.Entity<Student>().HasKey<int>(s => new { s.StudentKey, s.StudentName }); 
    }
}
        

Configure Column Name, Type and Order:

Default Code-First convention creates a column for a property with the same name, order, and datatype. You can override this convention, as shown below.

public class SchoolContext: DbContext 
{
    public SchoolDBContext(): base() 
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Configure Column
        modelBuilder.Entity<Student>()
                    .Property(p => p.DateOfBirth)
                    .HasColumnName("DoB")
                    .HasColumnOrder(3)
                    .HasColumnType("datetime2");
    }
}
        

As you can see in the above example, we used the Property() method to configure anything for a property of an entity. Here, we use HasColumnName to change the column name of DateOfBirth property. Also, we call HasColumnOrder and HasColumnType to change the order and datatype of a column.

modelBuilder.Entity<TEntity>().Property(expression) allows you to use different methods to configure a particular property, as shown below.

configure property with fluent api Entity Framework code-first

Configure Null or NotNull column for a property:

Code-First will create NotNull column for a primitive data type property because primitive data type can not be null unless it is marked as nullable using ? sign or Nullable<T>.

Use IsOptional method to create a nullable column for a property. In the same way, use IsRequired method to create a NotNull column.

namespace CodeFirst_FluentAPI_Tutorials
{
        
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
                //Configure Null Column
            modelBuilder.Entity<Student>()
                    .Property(p => p.Heigth)
                    .IsOptional();
                        
                //Configure NotNull Column
                modelBuilder.Entity<Student>()
                    .Property(p => p.Weight)
                    .IsRequired();
        }
    }
}
        

Configure Column Size:

Code-First will set the maximum size of a data type for a column. You can override this convention, as shown below.

namespace CodeFirst_FluentAPI_Tutorials
{
        
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Set StudentName column size to 50
            modelBuilder.Entity<Student>()
                    .Property(p => p.StudentName)
                    .HasMaxLength(50);
                        
            //Set StudentName column size to 50 and change datatype to nchar 
            //IsFixedLength() change datatype from nvarchar to nchar
            modelBuilder.Entity<Student>()
                    .Property(p => p.StudentName)
                    .HasMaxLength(50).IsFixedLength();
                        
            //Set size decimal(2,2)
                modelBuilder.Entity<Student>()
                    .Property(p => p.Height)
                    .HasPrecision(2, 2);
        }
    }
}
        

As you can see in the above example, we used HasMaxLength method to set the size of a column. IsFixedLength method converts nvarchar to nchar type. In the same way, HasPrecision method changed the precision of the decimal column.

Configure Concurrency Column:

You can configure a property as concurrency column using ConcurrencyToken method, as shown below.

namespace CodeFirst_FluentAPI_Tutorials
{
        
    public class SchoolContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Set StudentName as concurrency column
            modelBuilder.Entity<Student>()
                    .Property(p => p.StudentName)
                    .IsConcurrencyToken();
        }
    }
}
        

As you can see in the above example, we set StudentName column as concurrency column so that it will be included in the where clause in update and delete commands.

You can also use IsRowVersion() method for byte[] property to make it as a concurrency column.