实体框架数据库首先如何修改实体以使它们从基类派生 [英] Entity Framework Database first How to alter entities to make them derive from a base class

查看:106
本文介绍了实体框架数据库首先如何修改实体以使它们从基类派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经有数据库的数据库已经包含在其中的数据。我所看到的是所有表共有5列:




  • Id-long,key

  • IsDeleted,bit

  • DateDeleted,SmallDatetime

  • LastUpdated,SmallDatetime

  • LastUpdatedUser,nvarchar



现在,有一些常见的操作是基于当前在任何地方复制的字段完成的。



我想要的是创建一个只包含这些常见属性的基类和完成的方法,并使每个其他实体都从这个派生出来。



我不需要或想要在数据库本身拥有这个基础实体,这只是我想帮助编码部分。



问题是这是数据库第一,我不能更改数据库,所以我必须使用的是POCO类和EDMX。



我该如何实现?

解决方案

你正在寻找的是类似的东西到TPH( http://msdn.microsoft.com/en-us/data/jj618292.aspx



我不认为这可以适用于你,因为你有多个现有的表。



可能的解决方案之一是:


  1. 创建一个名为BaseModel的基类(或类似的东西)

  2. 将这些属性添加为抽象以强制它们被覆盖

  3. 在该基类中创建一个方法来填充这些字段,或创建一个帮助器,它使用BaseModel,IsDeleted,作为参数使用LastUpdated,LastUpdatedUser作为参数并更新模型。

  4. 扩展由模型生成的部分类 .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:

  1. Create a base class called "BaseModel" (or something like that)
  2. Add those properties as abstracts to force them to be overridden
  3. 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.
  4. Extend the partial classes generated by the model.tt file and inherit from the BaseModel class.

Thanks, Dave

这篇关于实体框架数据库首先如何修改实体以使它们从基类派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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