简体   繁体   English

实体框架6.1代码优先TPH / TPT混合映射问题

[英]Entity Framework 6.1 Code First TPH/TPT hybrid mapping issue

I have a model where most of the entities inherit the same base functionality. 我有一个模型,其中大多数实体都继承相同的基本功能。 That functionality is encapsulated in an abstract base class. 该功能封装在抽象基类中。 Above that, there are two branches of functionality and thus there are two abstract classes that inherit from the abstract base class. 除此之外,还有两个功能分支,因此有两个抽象类从抽象基类继承。 At this point, concrete classes inherit from their respective intermediate abstract classes. 此时,具体类从其各自的中间抽象类继承。

Below is an example of the classes and EF mapping for one branch of this type of model model: 以下是这种模型模型的一个分支的类和EF映射的示例:

public class TestContext : DbContext
{
    public DbSet<Base> Bases { get; set; }
    public DbSet<Intermediate> Intermediates { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Intermediate>().ToTable("Concretes");
        modelBuilder.Entity<Concrete1>().ToTable("Concretes");
        modelBuilder.Entity<Concrete2>().ToTable("Concretes");
    }
}
public abstract class Base
{
    public int Id { get; set; }
    public string BaseString { get; set; }
    public int BaseInteger { get; set; }
}

public abstract class Intermediate : Base
{
    public string IntermediateString { get; set; }
    public int IntermediateInteger { get; set; }
}

public class Concrete1 : Intermediate
{
    public string Concrete1String { get; set; }
    public int Concrete1Integer { get; set; }
}

public class Concrete2 : Intermediate
{
    public string Concrete2String { get; set; }
    public int Concrete2Integer { get; set; }
}

Where I am running into issues is with the table mapping. 我遇到问题的地方是表映射。 While mapping Intermediate , Concrete1 , and Concrete2 does generate a database- and even gives me a discriminator column- it does not make the columns nullable as is required for TPH mapping and therefore I end up with the following exception message: 虽然映射IntermediateConcrete1Concrete2确实会生成数据库-甚至给我一个鉴别符列-但它不会使列成为TPH映射所需的可为空,因此我最终得到以下异常消息:

(28,10) : error 3023: Problem in mapping fragments starting at line 28:Column Concretes.Concrete1Integer in table Concretes must be mapped: It has no default value and is not nullable. (28,10):错误3023:映射片段的问题始于第28行:表Concrete中的列Concrete.Concrete1Integer必须映射:它没有默认值,并且不能为空。

I've tried several permutations of this- 我已经尝试了几种排列方式-

  • Only mapping Intermediate to a custom table- works, but the concrete types' fields are mapped to the "Bases" table. 仅将Intermediate映射到自定义表,但是将具体类型的字段映射到“基础”表。
  • Mapping Intermediate to its own table and Concrete1 & Concrete2 to a table named "Concretes"- Exact same issues as the original mapping in the code example above. Intermediate映射到其自己的表,将Concrete1Concrete2映射到名为“ Concretes”的表-与上述代码示例中的原始映射完全相同。
  • Mapping every entity to it's own table - This works, but it's purely TPT which isn't what I want. 将每个实体映射到它自己的表-可以,但是纯TPT并不是我想要的。
  • Mapping nothing- this puts everything in the "Bases" table and is purely TPH, which for the real results in an unacceptably wide and sparse table. 什么都不映射-这会将所有内容都放入“基本”表中,并且纯属TPH,这对于真正的结果来说是一个无法接受的宽且稀疏的表。

Is there not some way to get the mapping I'm looking for in the code example? 在代码示例中,没有某种方法可以获取我正在寻找的映射吗? That is, a "Bases" table and a "Concretes" table that uses a discriminator? 也就是说,使用区分符的“基本”表和“具体”表?

As of version Entity Framework 6.1.0, this is an outstanding bug. 从Entity Framework 6.1.0版本开始,这是一个突出的错误。 It sounds like it will be a bug in 6.1.1 as well, but a fix has been scheduled for 6.1.2. 听起来这也将是6.1.1中的错误,但已计划在6.1.2中进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM