开发者

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

开发者 https://www.devze.com 2023-03-07 07:00 出处:网络
Imagine that I have UserId (actually we do have roughly four columns like userId, adddate, moddate etc. for almost all tables) column for almost 开发者_Go百科all of the entities.

Imagine that I have UserId (actually we do have roughly four columns like userId, adddate, moddate etc. for almost all tables) column for almost 开发者_Go百科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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消