如何在实体框架中定义所有实体的共同属性 [英] How to define common property(ies) for all entities in entity framework
问题描述
想象一下,我有几个所有实体的UserId(实际上我们有几乎所有表的userId,adddate,moddate等等)的列。
Imagine that I have UserId (actually we do have roughly four columns like userId, adddate, moddate etc. for almost all tables) column for almost all of the entities.
如何使这样的列在单独的实体(例如父实体)中可用,并使所有其他实体(子实体)从其继承?
How can I make such a column(s) to be available in a separate entity (say parent entity) and make all other entities (child entities) inheriting from it?
推荐答案
如果您使用生成的实体类(POCO或 EntityObject
)你将必须在你的模型(EDMX)中定义基本抽象实体,并在TPC继承中派生其他实体 - 根本不这样做,因为它会引入许多其他问题 - 例如,你将访问单个 ObjectSet
的基本类型,PK将必须在所有实体等中都是唯一的。
If you are using generated entity classes (either POCO or EntityObject
) you will have to define base abstract entity in your model (EDMX) and derive other entities in TPC inheritance - simply don't do that because it will introduce so many other problems - for example you will access all entities from the single ObjectSet
of the base type, PK will have to be unique among all your entities, etc.
另一种方法是使用定制POCO类(不生成)或自定义T4模板来创建公共对象继承,但仍然在设计器中使用普通实体而不继承。我以为这是不可能的,但几天前我用简单的例子测试了它,它的工作 - 但我仍然有点怀疑这种方法。
Another way is either using custom POCO classes (not generated) or custom T4 template to create common object inheritance but still using normal entities in the designer without inheritance. I thought that this is not possible but few days ago I tested it with simple example and it worked - but I'm still little bit suspicious to this approach.
这篇关于如何在实体框架中定义所有实体的共同属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!