The Fluent API can be used to configure an entity to map it with database table(s), default schema, etc.
First, let's configure a default schema for the tables in the database. However, you can change the schema while creating the individual tables. The following example sets the Admin schema as a default schema. All the database objects will be created under the Admin schema unless you specify a different schema explicitly.
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 default schema modelBuilder.HasDefaultSchema("Admin"); } }
Code-First will create the database tables with the name of DbSet
properties in the context class, Students
and Standards
in this case. You can override this convention and give a different table name than the DbSet
properties, 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) { //Configure default schema modelBuilder.HasDefaultSchema("Admin"); //Map entity to table modelBuilder.Entity<Student>().ToTable("StudentInfo"); modelBuilder.Entity<Standard>().ToTable("StandardInfo","dbo"); } } }
As you can see in the above example, we start with the Entity<TEntity>()
method. Most of the time, you have to start with the Entity<TEntity>()
method to configure it using Fluent API.
We have used the ToTable()
method to map the Student
entity to the StudentInfo
table and the Standard
entity to the StandardInfo
table. Notice that StudentInfo
is in the Admin schema and the StandardInfo
table is in the dbo schema because we have specified the dbo schema for the StandardInfo
table.
The following example shows how to map the Student
entity to multiple tables in the database.
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) { modelBuilder.Entity<Student>().Map(m => { m.Properties(p => new { p.StudentId, p.StudentName}); m.ToTable("StudentInfo"); }).Map(m => { m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth}); m.ToTable("StudentInfoDetail"); }); modelBuilder.Entity<Standard>().ToTable("StandardInfo"); } } }
As you can see in the above example, we mapped some properties of the Student
entity to the StudentInfo
table and other properties to the StudentInfoDetail
table using the Map()
method.
Thus, the Student
entity will split into two tables, as shown below.
The Map()
method requires a delegate method parameter. You can pass an Action delegate or a lambda expression in the Map()
method, as shown below.
using System.Data.Entity.ModelConfiguration.Configuration; 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) { modelBuilder.Entity<Student>().Map(delegate(EntityMappingConfiguration<Student> studentConfig) { studentConfig.Properties(p => new { p.StudentId, p.StudentName }); studentConfig.ToTable("StudentInfo"); }); Action<EntityMappingConfiguration<Student>> studentMapping = m => { m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth }); m.ToTable("StudentInfoDetail"); }; modelBuilder.Entity<Student>().Map(studentMapping); modelBuilder.Entity<Standard>().ToTable("StandardInfo"); } } }