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

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

问题描述

我正在尝试使用实体框架 6.0 和数据库优先方法来实现继承.好的,假设我有一个 Person 和一个 Organization 实体,如下所示:

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; }
}

我可以在数据库中创建这两个表,但我想使用继承,因此在 PersonOrganization 中重复的字段可以在另一个基类中,例如下面:

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?

推荐答案

一种可能的方法是为每种类型使用一张表,称为TPT (table-per-type),我更喜欢使用.为此,您可以像下图所示的模型一样定义表:

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:

请注意,子实体和基实体之间的关系在其 pk 列上是一对一,并且所有公共字段都移动到基表中.创建表后,右键单击 Visual Studio 中的模型页面,然后选择从数据库更新模型...,然后在添加选项卡中,选择要添加的这 3 个表.首先你应该看到这个模型图,它需要稍微改变一下:

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:

分别为PersonOrganization 执行这些步骤:

Do these steps for Person and Organization separately:

  • 右键单击实体并选择属性
  • 基本类型属性中选择Identity
  • 选择然后删除此实体与Identity之间的关联
  • 选择然后删除该实体的PK(ID列)(从基础实体继承)
  • 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!

其他资源:
实体框架设计器 TPH 继承

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

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