1
Reply

How to maping in Code First approach

Abhishek Kumar

Abhishek Kumar

Sep 27, 2017
362
1

    Code-First will create the database tables with the name of DbSet properties in the context class - Employees and Departments in this case. You can override this convention and can give a different table name than the DbSet properties, as shown below.namespace CodeFirst_Tutorials {public class EmployeeContext: DbContext {public EmployeeDBContext(): base() {}public DbSet Employees { get; set; }public DbSet Departments { get; set; }protected override void OnModelCreating(DbModelBuilder modelBuilder){//Configure default schemamodelBuilder.HasDefaultSchema("Admin");//Map entity to tablemodelBuilder.Entity().ToTable("EmployeeInfo");modelBuilder.Entity().ToTable("DepartmentInfo","dbo");}} }As you can see in the above example, we start with the Entity() method. Most of the time, you have to start with the Entity() method to configure it using Fluent API. We have used ToTable() method to map Employee entity to EmployeeInfo and Department entity to DepartmentInfo table. Notice that EmployeeInfo is in Admin schema and DepartmentInfo table is in dbo schema because we have specified dbo schema for DepartmentInfo table.

    Tushar Bharambe
    January 10, 2018
    0