.Net Core Entity Framework - 鉴别器 TPH
.Net Core Entity Framework - Discrimator TPH
我目前正在尝试写入从抽象基础 class 继承的 table。当我尝试这样做时,出现以下错误(ContactMethod 属性 是鉴别器):
System.Data.SqlClient.SqlException: Invalid column name 'ContactMethod'.
EmailContactDetails.cs:
public class EmailContactDetail : ContactDetail
{
[ApiMember(Description = "The Contact Method")]
public override ContactMethod ContactMethod => ContactMethod.Email;
[ApiMember(Description = "Email Address")]
public string EmailAddress { get; set; }
}
EmailContactDetailConfiguration.cs:
public class EmailContactDetailsConfiguration : IEntityTypeConfiguration<EmailContactDetail>
{
public void Configure(EntityTypeBuilder<EmailContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<EmailContactDetail> builder, string schema)
{
builder.Property(x => x.EmailAddress).HasColumnName("EmailAddress").HasColumnType("nvarchar(255)");
}
}
ContactDetail.cs:
public abstract class ContactDetail
{
[ApiMember(Description = "The Identifier")]
public Guid Id { get; set; }
[ApiMember(Description = "The Contact Method")]
public virtual ContactMethod ContactMethod { get; set; }
}
ContactDetailConfiguration.cs
public class ContactDetailsConfiguration : IEntityTypeConfiguration<ContactDetail>
{
public void Configure(EntityTypeBuilder<ContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<ContactDetail> builder, string schema)
{
builder.ToTable("ContactDetails", schema);
// Table per hierarchy. all subclasses share the same db table for performance.
builder.HasDiscriminator(x => x.ContactMethod)
.HasValue<EmailContactDetail>(ContactMethod.Email);
builder.Property(x => x.Id).HasColumnName("Id").IsRequired().HasColumnType("uniqueidentifier").ValueGeneratedOnAdd();
}
}
我尝试通过将以下内容添加到 ContactDetailConfiguration.cs 文件来隐藏鉴别器 "ContactMethod":
builder.Ignore(x => x.ContactMethod);
完成后,出现以下错误
The entity type 'EmailContactDetail' is part of a hierarchy, but does not have a discriminator property configured.
您不应向 EF 隐藏配置为 TPH 鉴别器的 属性,因为它对于 EF Core 实施 TPH 策略至关重要。
初始错误仅表示您的模型和数据库不同步。的确,按照惯例,EF Core 使用 string
shadow property 和名为 Discriminator
的列。但是 HasDiscriminator
fluent API 的全部目的是允许更改鉴别器 property/column 类型,以及将其映射到实体模型的现有 属性。
这里是这样的。您已告知 EF Core 使用现有 属性 ContactMethod
作为鉴别器,因此 EF Core 正在数据库 table 中查找名为 ContactMethod
的列。因此,要解决此问题,只需从模型更新数据库(在更改模型时使用通常的过程 - 添加新迁移、更新数据库等)。
我目前正在尝试写入从抽象基础 class 继承的 table。当我尝试这样做时,出现以下错误(ContactMethod 属性 是鉴别器):
System.Data.SqlClient.SqlException: Invalid column name 'ContactMethod'.
EmailContactDetails.cs:
public class EmailContactDetail : ContactDetail
{
[ApiMember(Description = "The Contact Method")]
public override ContactMethod ContactMethod => ContactMethod.Email;
[ApiMember(Description = "Email Address")]
public string EmailAddress { get; set; }
}
EmailContactDetailConfiguration.cs:
public class EmailContactDetailsConfiguration : IEntityTypeConfiguration<EmailContactDetail>
{
public void Configure(EntityTypeBuilder<EmailContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<EmailContactDetail> builder, string schema)
{
builder.Property(x => x.EmailAddress).HasColumnName("EmailAddress").HasColumnType("nvarchar(255)");
}
}
ContactDetail.cs:
public abstract class ContactDetail
{
[ApiMember(Description = "The Identifier")]
public Guid Id { get; set; }
[ApiMember(Description = "The Contact Method")]
public virtual ContactMethod ContactMethod { get; set; }
}
ContactDetailConfiguration.cs
public class ContactDetailsConfiguration : IEntityTypeConfiguration<ContactDetail>
{
public void Configure(EntityTypeBuilder<ContactDetail> builder) => Configure(builder, "dbo");
public void Configure(EntityTypeBuilder<ContactDetail> builder, string schema)
{
builder.ToTable("ContactDetails", schema);
// Table per hierarchy. all subclasses share the same db table for performance.
builder.HasDiscriminator(x => x.ContactMethod)
.HasValue<EmailContactDetail>(ContactMethod.Email);
builder.Property(x => x.Id).HasColumnName("Id").IsRequired().HasColumnType("uniqueidentifier").ValueGeneratedOnAdd();
}
}
我尝试通过将以下内容添加到 ContactDetailConfiguration.cs 文件来隐藏鉴别器 "ContactMethod":
builder.Ignore(x => x.ContactMethod);
完成后,出现以下错误
The entity type 'EmailContactDetail' is part of a hierarchy, but does not have a discriminator property configured.
您不应向 EF 隐藏配置为 TPH 鉴别器的 属性,因为它对于 EF Core 实施 TPH 策略至关重要。
初始错误仅表示您的模型和数据库不同步。的确,按照惯例,EF Core 使用 string
shadow property 和名为 Discriminator
的列。但是 HasDiscriminator
fluent API 的全部目的是允许更改鉴别器 property/column 类型,以及将其映射到实体模型的现有 属性。
这里是这样的。您已告知 EF Core 使用现有 属性 ContactMethod
作为鉴别器,因此 EF Core 正在数据库 table 中查找名为 ContactMethod
的列。因此,要解决此问题,只需从模型更新数据库(在更改模型时使用通常的过程 - 添加新迁移、更新数据库等)。