2011-06-09 84 views
6

您如何明確地告訴EF某個表位於特定模式中?引用實體框架中的表的模式名稱

例如,AdventureWorks數據庫定義了Production.Product表。當使用OnModelCreating方法,我用下面的代碼:

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

然而,在運行時,它說,它Invalid object name: dbo.Product

我曾嘗試:

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

,但都失敗也是如此。

回答

12

ToTable具有重載版本,它有兩個參數:表名和模式名稱,以便正確的版本是:

config.ToTable("Product", "Production");