C# 在实体框架中引用表的架构名称
如何明确地告诉EF一个表位于一个特定的模式中 例如,AdventureWorks数据库定义Production.Product表。使用C# 在实体框架中引用表的架构名称,c#,entity-framework-4.1,data-access,C#,Entity Framework 4.1,Data Access,如何明确地告诉EF一个表位于一个特定的模式中 例如,AdventureWorks数据库定义Production.Product表。使用OnModelCreating方法时,我使用以下代码: protected override void OnModelCreating(DbModelBuilder modelBuilder) { EntityTypeConfiguration<Product> config = modelBuilder.Entity<Product&g
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");
}
但两者都失败。
ToTable
具有重载版本,该版本接受两个参数:表名和架构名,因此正确的版本为:
config.ToTable("Product", "Production");
还可以使用数据注释,使用“table”属性上的可选参数“schema”,指定表架构
using System.ComponentModel.DataAnnotations.Schema;
[Table("Product", Schema="Production")]
public class Product
{
public int ProductID { get; set; }
public decimal Price { get; set; }
}
using System.ComponentModel.DataAnnotations.Schema;
[Table("Product", Schema="Production")]
public class Product
{
public int ProductID { get; set; }
public decimal Price { get; set; }
}