实体框架数据库第一如何改变实体,使他们从一个基类派生 [英] Entity Framework Database first How to alter entities to make them derive from a base class

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

问题描述

我有一个数据库已经完全在他们的数据表。我所看到的是,所有的表有5列是共同的:

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-长,键
  • 请将isDeleted,有点
  • 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.

问题是,这是数据库首先,​​我不能改变数据库,因此所有我有工作,是POCO类和EDMX。

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?

推荐答案

您在找什么类似于TPH的东西(的 http://msdn.microsoft.com/en-us/data/jj618292.aspx

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. 创建名为BaseModel一个基类(或类似的东西)
  2. 在添加这些属性为文摘迫使他们重写
  3. 创建在基类中的方法来填充这些字段,或创建一个帮助这需要BaseModel,请将isDeleted,LASTUPDATED,LastUpdatedUser作为参数,并更新模型。
  4. 扩展了的模式的.TT文件生成的部分类,并从BaseModel类继承。
  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.

谢谢, 戴夫

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

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