类继承自Entity Framework类,仍然可以使用继承的类映射来保留到DB中? [英] Can a class inherit from Entity Framework class and still persist back to the DB using the inherited classes mappings?

查看:158
本文介绍了类继承自Entity Framework类,仍然可以使用继承的类映射来保留到DB中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个类可以继承自Entity Framework类,并且仍然使用继承的类映射来保存到DB中?尝试从自我跟踪实体类派生时,我收到错误,并且SaveChanges表示该类型没有任何映射。我希望自从类型从Entity类继承以来,它也可以以某种方式继承实体映射,使其工作。有没有人能够得到这个工作?



我想要支持的方案是将Entity对象扩展到引用包含实体和映射的程序集的另一个程序集中。 Partials类和映射被编译到包含Entity对象的程序集中。所以我不能通过使用部分类完成这个。

解决方案

不可以用EDMX(和自我跟踪实体)。如果要存储派生类型,它也必须进行映射。如果您需要添加任何生成的代码,请使用您自己的部分实体类。



令人惊讶的是,它看起来像代码优先(EFv4 .1和DbContext API),但在这种情况下,您不能使用自我跟踪实体。我刚检查数据库,并且代码优先也是不可能的。它默默地创建TPH继承,派生类被映射为另一个实体。


Can a class inherit from Entity Framework class and still persist back to the DB using the inherited classes mappings? I have received errors trying to derive from a 'Self-Tracking Entity' class when I try and SaveChanges stating that the Type does not have any mappings. I would have hoped since the Type was Inherited from the Entity class that it could also inherit the entity mappings somehow for this to work. Has anyone been able to get this to work?

The scenario I am trying to support is to extend the Entity object in another assembly that references the assembly containing the Entity's and mappings. Partials classes and mappings are compiled into the assembly containing the Entity objects. So I can't accomplish this through the use of partial classes.

解决方案

No it is not possible with EDMX (and self tracking entities). If you want to store derived type it must be mapped as well. If you need to add anything to generated code use your own partial part of the entity class.

Surprisingly it looks like it is possible with code-first (EFv4.1 and DbContext API) but in such case you cannot use self tracking entities. I just checked database and it is not possible with code-first as well. It silently creates TPH inheritance and derived class is mapped as an another entity.

这篇关于类继承自Entity Framework类,仍然可以使用继承的类映射来保留到DB中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆