如何在 EF 代码优先中映射继承的实体

How to map inherited entities in EF code-first(如何在 EF 代码优先中映射继承的实体)
本文介绍了如何在 EF 代码优先中映射继承的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 AdventureWorks 2012 示例数据库映射到 EF 6.1.3 代码优先数据层,但我不知道如何映射 EmployeePerson 实体.Employee 显然应该从 Person 派生,具有 EMPerson.PersonType,但我不知道如何使用 进行映射EntityTypeConfiguration映射"类.知道了这一点,我还可以将 Person 映射为从 BusinessEntity 派生.

I'm trying to map the AdventureWorks 2012 sample database to an EF 6.1.3 code-first data layer, and am stuck at how to map the Employee and Person entities. Employee should apparently derive from Person, with a Person.PersonType of EM, but I don't know how to map this using EntityTypeConfiguration<TEntity> 'mapping' classes. Knowing this, I could also map Person to derive from BusinessEntity.

推荐答案

我将解释如何使用 Code First 方法执行 EmployeePerson 表之间的映射,您可以按照相同的过程来映射 BusinessEntityPerson 之间的继承.

I will explain how to perform the mapping between the Employee and the Person tables using Code First approach, you can follow the same procedure to map inheritance between BusinessEntityand Person.

使用的继承映射策略是 TPT(Table Per Type),我创建了一个简单的控制台应用程序,安装 AdventureWorks2012 数据库后,我按照 EF DataModel 向导生成代码优先类,我将修改这些类以映射继承,所以这是结果代码:

The inheritance mapping strategy used is TPT (Table Per Type), I've created a simple Console Application, With the AdventureWorks2012 Database installed, I followed the EF DataModel Wizard to generate the code first classes that I will modify to map the inheritance, So here is the resulted code:

Person 类:

public  partial class Person
{
    [Key]
    public int BusinessEntityID { get; set; }

    [Required]
    public string PersonType { get; set; }

    public bool NameStyle { get; set; }

    public string Title { get; set; }

    [Required]
    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    [Required]
    public string LastName { get; set; }

    public string Suffix { get; set; }

    public int EmailPromotion { get; set; }

    [Column(TypeName = "xml")]
    public string AdditionalContactInfo { get; set; }

    [Column(TypeName = "xml")]
    public string Demographics { get; set; }

}

员工类:

 public partial class Employee: Person
{

    [Required]
    public string NationalIDNumber { get; set; }

    [Required]
    public string LoginID { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public short? OrganizationLevel { get; set; }

    [Required]
    public string JobTitle { get; set; }

    [Column(TypeName = "date")]
    public DateTime BirthDate { get; set; }

    [Required]
    public string MaritalStatus { get; set; }

    [Required]
    public string Gender { get; set; }

    [Column(TypeName = "date")]
    public DateTime HireDate { get; set; }

    public bool SalariedFlag { get; set; }

    public short VacationHours { get; set; }

    public short SickLeaveHours { get; set; }

    public bool CurrentFlag { get; set; }

    public Guid rowguid { get; set; }

    public DateTime ModifiedDate { get; set; }
}

最后是 AW 上下文类:

And Finally the AW Context Class:

 public partial class AW : DbContext
{
    public AW()
        : base("name=AWConnectionString")
    {
    }

    public virtual DbSet<Employee> Employees { get; set; }

    public virtual DbSet<Person> People { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {            
        modelBuilder.Entity<Person>().ToTable("Person.Person");
        modelBuilder.Entity<Employee>().ToTable("HumanResources.Employee");
    }
}

一个简单的测试(对我有用;)):

A simple test (That works for me ;) ):

 class Program
{
    static void Main(string[] args)
    {
        using(var db= new AW())
        {
            var e = db.Employees.First();
            e.JobTitle = "Web Developper";
            db.SaveChanges();
        }
    }
}

可以参考这篇文章了解更多详情

You can refer to this article for more details

这篇关于如何在 EF 代码优先中映射继承的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)