[英]EF Core fluent mapping to inner object properties
I have a class that contains some properties. 我有一个包含一些属性的类。 For some architectural reasons, I have an instance of another objet into my class.
出于某些架构原因,我在我的班级中有另一个对象的实例。
Simple example 简单的例子
public class MyEntity {
public MySubEntity SubEntity {get; set;}
}
For this, I create fluent mapping like : 为此,我创建了流畅的映射,如:
builder.ToTable(MyEntity.CONST_TABLE_NAME);
builder.HasKey(m => m.Id);
builder.Property(m => m.Column1).IsRequired();
builder.Property(m => m.SubEntity.Column2).IsRequired();
I cannot integrate all my subEntity properties into my main entity (my subEntity has its own intelligence). 我无法将所有subEntity属性集成到我的主实体中(我的subEntity有自己的智能)。 I just want to map my subentity properties, which is NOT stored in a separated table, to myEntity table.
我只想将我的子实体属性(不存储在单独的表中)映射到myEntity表。
The last line throw an exception : 最后一行抛出异常:
The expression 'm => m.SubEntity.Column2' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.
How can I perform such mapping ? 我该如何进行这种映射?
EF Core doesn't support this type of mapping for now. EF Core目前不支持此类映射。 It will not be supported in EF Core 1.0 RTM (see my github issue : https://github.com/aspnet/Home/issues/1330 )
EF Core 1.0 RTM不支持它(请参阅我的github问题: https : //github.com/aspnet/Home/issues/1330 )
As I described in my github issue, I figured out 2 solutions : 正如我在github问题中描述的那样,我找到了2个解决方案:
1) Create a derived class from my model, specialy designed for EF, and expose all properties as simple. 1)从我的模型中创建一个派生类,专门为EF设计,并将所有属性公开为简单。 It will need more mapping when insert/update and retrieve from Db.
插入/更新和从Db检索时需要更多映射。 We don't choose this option
我们不选择此选项
2) Create proxy properties. 2)创建代理属性。 In my example, this is like :
在我的例子中,这就像:
public class MyEntity {
private MySubEntity SubEntity {get; set;}
public string SubEntityValue
{
get
{
return SubEntity.Value;
}
set
{
SubEntity.Value = value;
}
}
This seems to be the best solution (we choose this one). 这似乎是最好的解决方案(我们选择这个)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.