问题描述
我有一个包含一些属性的类。
对于一些建筑方面的原因,我有另外一个客体的实例到我的课。
I have a class that contains some properties.For some architectural reasons, I have an instance of another objet into my class.
简单的例子
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();
我无法将所有子图的属性融入我的主要实体(我的子实体有它自己的智能)。我只是要地图我的子实体的属性,这是不存储在分隔的表,要myEntity所表
I cannot integrate all my subEntity properties into my main entity (my subEntity has its own intelligence). I just want to map my subentity properties, which is NOT stored in a separated table, to myEntity table.
最后一行抛出一个异常:
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核心不支持这种类型的映射现在。它不会在EF核心支持1.0 RTM(见我的github问题:)
EF Core doesn't support this type of mapping for now. It will not be supported in EF Core 1.0 RTM (see my github issue : https://github.com/aspnet/Home/issues/1330)
我在github上描述的问题,我想通了2解决方案:
As I described in my github issue, I figured out 2 solutions :
1)我的模型,specialy设计的EF创建一个派生类,并公开所有属性一样简单。这将需要更多的映射时插入/更新,并从数据库中检索。我们不选择此选项
1) Create a derived class from my model, specialy designed for EF, and expose all properties as simple. It will need more mapping when insert/update and retrieve from Db. We don't choose this option
2)创建代理属性。在我的例子,这是这样的:
2) Create proxy properties. 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).
这篇关于EF核心流利的映射到内对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!