Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing schema names for tables in the Entity Framework

How do you explicitally tell EF that a table lies in a specific schema?

For example, the AdventureWorks database defines the Production.Product table. When using the OnModelCreating method, I use the following code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    EntityTypeConfiguration<Product> config = modelBuilder.Entity<Product>();

    config.HasKey(p => p.ProductID);
    config.Property(p => p.Price).HasColumnName("ListPrice");
    config.ToTable("Product");
}

However, when it is run, it says it Invalid object name: dbo.Product.

I have tried:

config.ToTable("Production.Product");
//and
config.HasEntityName("Production");

but both fail as well.

like image 229
Dominic Zukiewicz Avatar asked Jun 09 '11 11:06

Dominic Zukiewicz


1 Answers

ToTable has overloaded version which accepts two parameters: table name and schema name so correct version is:

config.ToTable("Product", "Production");
like image 166
Ladislav Mrnka Avatar answered Sep 19 '22 13:09

Ladislav Mrnka