Home > ADO.NET, C#, Entity Framework > How to specify table schema name with Entity Framework Code First

How to specify table schema name with Entity Framework Code First

07/01/2013

Using Entity Framework Code First approach, if we need to specify a schema name other than the default dbo, we need to override the OnModelCreating method of DbContext class:

public class MyDbContext : DbContext
{
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<MyTable1Entity>().ToTable("MyTable1", schemaName: "myschema");
    base.OnModelCreating(modelBuilder);
  }
}

In this example, we map the MyTable1Entity object to MyTable1 table, specifying that it is contained in the myschema schema.

Categories: ADO.NET, C#, Entity Framework
%d bloggers like this: