[英]FluentAPI mapping with POCO in EF4.1
I'm learning EF4.1 against the northwind database. 我正在针对罗斯文数据库学习EF4.1。 I have created a POCO class like this intentionally with non-identical column names and properties that don't exist in the scheme so I can learn mapping in preparation for updating my legacy app: 我故意创建了这样的POCO类,其中包含方案中不存在的不相同的列名和属性,因此我可以学习映射以准备更新我的旧版应用程序:
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public Decimal? UnitPrice { get; set; }
public bool Discontinued { get; set; }
public Int16 QuantityInStock { get; set; }
public int CategoryID { get; set; }
public Category Category { get; set; }
}
I want to map my schema to this entity like this: 我想像这样将我的架构映射到该实体:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Map(config =>
{
config.Properties(p => new
{
p.ProductName,
p.ProductID,
p.CategoryID,
p.Discontinued,
p.UnitPrice
});
});
modelBuilder.Entity<Product>().ToTable("Products");
base.OnModelCreating(modelBuilder);
}
Strangely, when I try this: 奇怪的是,当我尝试这个:
Product tmp = db.Products.Find(4);
I get this exception and I can't tell why since I'm referring to and I even mapped it ToTable("Products"): {"Invalid object name 'dbo.Product1'."}
我得到了这个异常,我不能说出原因,因为我正在引用,甚至将其映射到ToTable(“ Products”): {"Invalid object name 'dbo.Product1'."}
Why is this happening? 为什么会这样呢?
With modelBuilder.Entity<Product>().Map
you are using a quite advanced mapping option in Code-First called Table Splitting . 使用modelBuilder.Entity<Product>().Map
您在Code-First中使用了一个非常高级的映射选项,称为Table Splitting 。 Your mapping says that the properties of your entity Product
you have listed in Properties(p => new...)
should be mapped to another table than the rest of the properties. 您的映射表示您在“ Properties(p => new...)
列出的实体Product
Properties(p => new...)
应映射到另一个表,而不是其余属性。 The rest of the properties is in table Products
as you have defined in your ToTable
call. 其余属性位于您在ToTable
调用中定义的Products
表中。 For the other properties you don't have specified a table name at all (which should be ToTable
within the Map(config => config.ToTable(...) ...
action). My guess is that EF assumes some kind of default table name Product1
which apparently doesn't exist. 对于其他属性,您根本没有指定表名(在Map(config => config.ToTable(...) ...
操作中应为ToTable
)。我的猜测是EF假设某种默认表名Product1
显然不存在。
I'm not sure if you really want to split your entity to two different tables. 我不确定您是否真的想将实体拆分为两个不同的表。 Reading your first sentences... 阅读您的第一句话...
... non-identical column names and properties that don't exist in the scheme ... 方案中不存在的不相同的列名和属性
... I think you need mainly the following two mapping options: ...我认为您主要需要以下两个映射选项:
Properties in the model class without corresponding columns in the database are not mapped properties: 没有数据库中相应列的模型类中的属性不是映射的属性:
modelBuilder.Entity<Product>().Ignore(p => p.QuantityInStock);
With data annotations: 带有数据注释:
[NotMapped]
public Int16 QuantityInStock { get; set; }
And you can map a property name to another column name by: 您可以通过以下方式将属性名称映射到另一个列名称:
modelBuilder.Entity<Product>().Property(p => p.Discontinued)
.HasColumnName("MyOtherColumnName");
With data annotations: 带有数据注释:
[Column("MyOtherColumnName")]
public bool Discontinued { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.