How to define common property(ies) for all entities in entity framework

风格不统一 提交于 2019-12-11 07:12:30

问题


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?


回答1:


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.

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.




回答2:


Try using T4 templates like it is described here or here, modify the template to contain a base class (inherited from EntityObject) having these properties, and inherit each class from this base class.



来源:https://stackoverflow.com/questions/6064138/how-to-define-common-propertyies-for-all-entities-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!