gpt4 book ai didi

entity-framework - TPT 之上的 Entity Framework TPH

转载 作者:行者123 更新时间:2023-12-04 08:34:59 28 4
gpt4 key购买 nike

这个问题会围绕AccountIndividualAccountDoctor这3个class进行说明:

  • 前两个类是抽象的
  • IndividualAccount 是 Account 的子类
  • Doctor 是 IndividualAccount 的子类

第一层继承(在 Account 和 IndividualAccount 之间)是使用Table Per Type 方法实现的。第二层继承(在 IndividualAccount 和 Doctor 之间)正在使用 Table per Hierarchy 方法实现。

Fluent API配置如下:

class AccountConfiguration : EntityTypeConfiguration<Account>
public AccountConfiguration()
{
HasKey(x => x.Id);
...
}
}

class IndividualAccountConfiguration : EntityTypeConfiguration<IndividualAccount>
{
public IndividualAccountConfiguration()
{
ToTable("IndividualAccounts");
...
}
}

class DoctorConfiguration : EntityTypeConfiguration<Doctor>
{
public DoctorConfiguration()
{
...
}
}

我们希望从这个配置中得到两个表:第一个存储所有用户的 Id 和公共(public)属性(例如用户名和密码),第二个存储所有个人的公共(public)属性(例如姓名和电话号码)。此外,第二张表将有一个鉴别器字段,用于区分医生和我们领域中可能拥有的其他类型的个人。

当我试图通过它的 id 获得 Doctor 时,问题就出现了。将抛出一个异常,声称许多列无效,其中最重要的一个是:
列名“鉴别器”无效。\r\n

令我惊讶的是,如果我将 [Table("IndividualAccounts")] 放在 IndividualAccount 类定义之上,问题就会得到解决。但是我已经在配置(Fluent API)中设置了表名。除了 Fluent API 之外,为什么还要使用注解?

更新
在注释存在的情况下,Doctor 的属性被放置在 IndividualAccounts 表中,这确实是我们所期望的。但是如果我删除注释,将创建一个新的迁移,尝试将这些字段移动到基表 Account!

最佳答案

To my surprise if I place [Table("IndividualAccounts")] above IndividualAccount class definition, problem will be resolved. But I have already set the table name in configuration (Fluent API). Why should I use the annotation in addition to the Fluent API?

不,问题没有解决,现在的问题是隐藏的。用 SSM 检查一下,IndividualAccounts 表不包含 Discriminator 列!

ToTable("IndividualAccounts");

抛出UnsupportedHybridInheritanceMapping异常,只需启用所有异常,然后您将得到:

更多信息:

Multiple inheritance with Entity Framework TPC


我修改了我的类,现在我也可以看到鉴别器了。

下次请将您的类(class)与问题一起发布。更多信息:How to create a Minimal, Complete, and Verifiable example

如果您使用下面的行代码,这将禁用约定异常,但稍后您会遇到另一种问题:

modelBuilder.Conventions.Remove<MappingInheritedPropertiesSupportConvention>();

EF 尝试创建 TPC 而不是 TPT 的问题我会联系 EF-Team 并给你反馈。

关于entity-framework - TPT 之上的 Entity Framework TPH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330948/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com