Dom*_*icz 6 c# data-access entity-framework-4.1
你如何明确地告诉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");
}
Run Code Online (Sandbox Code Playgroud)
然而,当它运行时,它说Invalid object name: dbo.Product.
我试过了:
config.ToTable("Production.Product");
//and
config.HasEntityName("Production");
Run Code Online (Sandbox Code Playgroud)
但两者都失败了.
Lad*_*nka 12
ToTable 有重载的版本,它接受两个参数:表名和模式名,所以正确的版本是:
config.ToTable("Product", "Production");
Run Code Online (Sandbox Code Playgroud)