实体框架数据库首先如何修改实体以使它们从基类派生 [英] Entity Framework Database first How to alter entities to make them derive from a base class
问题描述
- Id-long,key
- IsDeleted,bit
- DateDeleted,SmallDatetime
- LastUpdated,SmallDatetime
- LastUpdatedUser,nvarchar
现在,有一些常见的操作是基于当前在任何地方复制的字段完成的。
我想要的是创建一个只包含这些常见属性的基类和完成的方法,并使每个其他实体都从这个派生出来。
我不需要或想要在数据库本身拥有这个基础实体,这只是我想帮助编码部分。
问题是这是数据库第一,我不能更改数据库,所以我必须使用的是POCO类和EDMX。
我该如何实现?
你正在寻找的是类似的东西到TPH( http://msdn.microsoft.com/en-us/data/jj618292.aspx)
我不认为这可以适用于你,因为你有多个现有的表。
可能的解决方案之一是:
- 创建一个名为BaseModel的基类(或类似的东西)
- 将这些属性添加为抽象以强制它们被覆盖
- 在该基类中创建一个方法来填充这些字段,或创建一个帮助器,它使用BaseModel,IsDeleted,作为参数使用LastUpdated,LastUpdatedUser作为参数并更新模型。
- 扩展由模型生成的部分类 .tt文件,并继承自BaseModel类。
谢谢,
Dave
I have a database already full of tables with data in them. What i have seen is that all tables have 5 columns in common:
- Id-long, key
- IsDeleted, bit
- DateDeleted, SmallDatetime
- LastUpdated, SmallDatetime
- LastUpdatedUser, nvarchar
Now, there are some common operations that are done based on those fields which currently are replicated everywhere
What i want is to create a base class containing just these common attributes and the methods done to them and make every other entity derive from this.
I dont need or want to have this base entity on the database per se, this is just something i want to help the coding part.
The issue is this is Database first, i cannot alter the database so all i have to work with are the POCO classes and the EDMX.
How can i achieve this?
What you are looking for is something similar to TPH (http://msdn.microsoft.com/en-us/data/jj618292.aspx)
I don't think this will work for you however, as you have multiple existing tables.
One of the possible solutions is:
- Create a base class called "BaseModel" (or something like that)
- Add those properties as abstracts to force them to be overridden
- Create a method in that base class to populate those fields, or create a helper which takes BaseModel, IsDeleted,LastUpdated, LastUpdatedUser as a parameter and update the model.
- Extend the partial classes generated by the model.tt file and inherit from the BaseModel class.
Thanks, Dave
这篇关于实体框架数据库首先如何修改实体以使它们从基类派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!