将派生类型映射到 EF 中的同一个表
以下简单的代码示例说明了相关场景。我有一个 Person 实体,它只是映射到数据库中的 Person 表。我正在使用默认的实体对象代码生成器。
public partial class Person { ... }
我有一个从 Person 派生的 PersonDetail 类,其中包含 GUI 所需的一些额外属性。这些属性都不需要保留在数据库中,因此我不想将此类映射到数据库。
public class PersonDetail : Person
{
public int TheGuiNeedsThisInformation { get; set; }
}
我使用 WCF 将 Detail 类的实例发送到 GUI。 我的问题是,如果 GUI 修改了 PersonDetail 实例的某些属性,并将其发送回服务器进行更新,我不能简单地将其附加到我的上下文,因为派生类没有映射到任何表(“映射和找不到 EntityType PersonDetail 的元数据信息”)。我尝试将其映射到与 Person 类映射相同的表,但 EF 会以这种方式抛出一些有关映射的异常。
我设法通过创建 PersonDetail 实例的副本来解决此问题,方法是将其属性值复制到新的 Person 实例,然后将其保存到数据库。但是我想避免额外的工作。我做错了什么?这不可能吗?在 Linq to SQL 中,这就像一个魅力,基本上是开箱即用的。
如果这是不可能的,那么建议的方法是什么来使用一些不需要保留的额外信息来扩展我的实体类? (显而易见的方法是简单地向部分 Person 类添加额外的属性。但是,我不想这样做,因为这样 GUI 永远无法确定额外的属性是否已在服务器上填充。)
更新:感谢您的建议,但是我的主要问题仍然悬而未决:是否可以将基类和派生类(其中派生类没有任何必须保存到数据库的附加属性)映射到相同的表,这样我就可以简单地将派生类型的实例附加到我的上下文并像它是基本类型的实例一样保存它?我还无法做到这一点,我很惊讶,因为使用 Linq to SQL 它就可以工作了。
The following simple code example illustrates the scenario in question. I have a Person entity, which is simply mapped to the Person table in the DB. I am using the default Entity Object code generator.
public partial class Person { ... }
I have a PersonDetail class deriving from Person, which contains some extra properties, which are needed by the GUI. None of these properties needs to be persisted in the DB, so I do not want to map this class to the database.
public class PersonDetail : Person
{
public int TheGuiNeedsThisInformation { get; set; }
}
And I send down instances of the Detail class the GUI with WCF.
My problem is, if the GUI modifies some properties of the PersonDetail instance, and sends it back to the server to update it, I can not simply Attach it to my context, because the derived class is not mapped to any table ("Mapping and metadata information could not be found for EntityType PersonDetail"). I tried to map it to the same table as the Person class is mapped, but the EF throws some exception about the mapping that way.
I managed to workaround this by creating a copy of the PersonDetail instance by copying its property values to a new Person instance, and then saving it to the DB. However I would like to avoid that extra work. What am I doing wrong? Is this not possible? In Linq to SQL this worked like a charm, basically out of the box.
And if this is not possible, what is the suggested way to extend my entity classes with some extra information, which don't have to be persisted? (The obvious way would be to simply add extra properties to the partial Person class. However, i do not want to do that, because that way the GUI can never be sure whether the extra properties have been filled on the server or not.)
UPDATE: Thanks for the suggestions, however my main question is still open: Is it possible to have a base and a derived class (where the derived class does not have any additional properties which have to be saved to the DB) mapped to the same table, so that I can simply attach an instance of the derived type to my context and save it like if it was an instance of the base type? I could not do this yet, and I am surprised, because with Linq to SQL it simply worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为继承不是这个问题的正确答案。 “PersonDetail”真的是一个人吗?
我会选择创建一个 PersonDetail 类,并在分部类中添加一个指向 PersonDetail 类实例的属性。
这样,您可以通过检查属性 != null 来在客户端检查 PersonDetail 是否由服务器设置。
I don't think inheritance is the right answer to this. Is it true that 'PersonDetail' is-a Person?
I would choose for creating a PersonDetail class and adding a property in the partial class that points to an instance of the PersonDetail class.
That way you can check on your client side if the PersonDetail is set by the server by just checking if the property != null.
您可以将属性添加到类中,并使用 modelBuilder 在实体框架中“忽略”它们,看起来就像您在进行代码优先开发。对于该选项,请查看 modelBuilder =>
希望这有帮助!
You can add your properties to the class and "ignore" them for the entity framework using the modelBuilder, looks like you doing code first development. For that options have a look at the modelBuilder =>
hope this helps!
您是否尝试过使用组合而不是继承?
除非有充分的理由,否则我永远不会将该对象转移到您的服务中。对我来说,这看起来只是一个客户端对象。当 GUI 属性没有存储在数据库中时,将它们传输到服务器有什么意义呢?仅仅
获取
Person 并将其发送到服务器进行更新还不够吗?Have you tried using composition instead of inheritance?
Unless there's a really good reason I would never transfer that object to your service however. To me that looks like a client side object only. What's the point of having GUI properties transmitted to the server when they're not stored in the database anyway? Isn't it enough to just
get
the Person and send that to the server for update?