Archive for the ‘Entity Framework’ Category

How to specify table schema name with Entity Framework Code First

07/01/2013 Comments off

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");

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