实体框架DB-First,实现继承 [英] Entity Framework DB-First, implement inheritance

查看:120
本文介绍了实体框架DB-First,实现继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用实体框架6.0和数据库第一种方法实现继承。好的,假设我有一个 Person 和一个组织实体,如下所示:

  //组织实体的简化版本
public class Organization
{
public Guid ID {得到;组; }
public string昵称{get;组; }
public string Email {get;组; }
public string PhoneNumber {get;组; }
public string OfficialName {get;组; }
public Guid CEOID {get;组; }
public DateTime? RegisterDate {get;组;
}

//个人实体的简化版本
public class Person
{
public Guid ID {get;组; }
public string昵称{get;组; }
public string Email {get;组; }
public string PhoneNumber {get;组; }
public Guid PersonID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string MiddleName {get;组; }
public string NationalCode {get;组; }
public DateTime? BirthDate {get;组;
}

我可以在数据库中创建这两个表,但是我想使用继承所以在 Person Organization 中重复的字段可能在另一个基类中,如下所示:

  public class Identity 
{
//这些字段是Person和组织
public Guid ID {get;组; }
public string昵称{get;组; }
public string Email {get;组; }
public string PhoneNumber {get;组;
}

如何在db-first方法中实现?

解决方案

一种可能的方式是使用一个表 每种类型 TPT (table-per-type),我喜欢使用。要实现这一点,您可以像下图所示的模型来定义表:





请注意,儿童和基础实体之间的关系在其pk上一对一列,并将所有常用字段移动到基表。创建表后,右键单击可视化工作室中的模型页面,然后从数据库中选择更新模型... ,然后在添加选项卡中选择要添加的3个表。起初你应该看到这个模型图,需要改变一下:





执行 Person 组织分开:




  • 右键单击实体,然后选择属性 li>
  • 基本类型属性中选择身份

  • 选择然后删除该实体与 Identity

  • 之间的关联选择然后删除PK (ID列)该实体(从基础实体继承)



执行这些步骤后,保存模型。现在你的模型应该是这样的:





现在编译你的项目,享受你的生活!



PS 感谢任何人可以编辑我的答案,并解释实体框架中的 TPH (按层次结构)继承。



希望这有帮助。 / p>

I'm trying to implement inheritance using entity framework 6.0 and database first approach. OK, let's say I have a Person and an Organization entity like below:

// a simplified version of organization entity
public class Organization
{
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string OfficialName { get; set; }
    public Guid CEOID { get; set; }
    public DateTime? RegisterDate { get; set; }
}

// a simplified version of person entity
public class Person
{
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public Guid PersonID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string NationalCode { get; set; }
    public DateTime? BirthDate { get; set; }
}

I can create these two tables in database, but I want to use inheritance so the fields which is repeated in both Person and Organization could be in another base class like below:

public class Identity
{
    // These fields are the common fields between Person and Organization
    public Guid ID { get; set; }
    public string Nickname { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
}

How can I achieve this in db-first approach?

解决方案

One possible way is to use one table for each type called TPT (table-per-type), which I prefer to use. To achieve this, you define your tables like the model shown in the following picture:

Note that the relationships between child and base entity are one-to-one on their pk columns, and all common fields are moved to the base table. After creating your tables, right click on the models page in your visual studio, and select Update Model from Database..., and then in the add tab, select these 3 tables to add. At first you should see this model diagram, which needs to be changed a bit:

Do these steps for Person and Organization separately:

  • Right click on entity and select Properties
  • In the Base Type property select Identity
  • Select and then delete the association between this entity and Identity
  • Select and then Delete the PK (ID column) of this entity (Inherits from base entity)

After these steps save your model. Now your model should look like this:

Now compile your project and enjoy your life!

P.S. I appreciate if anyone could edit my answer and explain the TPH (table-per-hierarchy) inheritance in entity framework.

Hope this helps.

这篇关于实体框架DB-First,实现继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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