EF核心“实体类型未配置区分属性”

时间:2018-07-25 10:35:59

标签: asp.net-core entity-framework-core ef-core-2.0

我有这样的模型结构

public abstract class BaseEntity
{
    [Key]
    public int Id { get; set; }
}
public abstract class AbstractRegistration : BaseEntity
{
    public int UserId { get; set; }
}
public class Registration1 : AbstractRegistration
{
    public string ReferenceNumber { get; set; }
}
public class Registration2 : AbstractRegistration
{
}

配置部分为

public class AbstractRegistrationConfig : IEntityTypeConfiguration<AbstractRegistration>
{
    public void Configure(EntityTypeBuilder<AbstractRegistration> builder)
    {
        // map table and schema
        builder.ToTable("tbl_reg");

        // map properties with actual columns
        builder.Property(t => t.Id).HasColumnName("id");
        builder.Property(t => t.UserId).HasColumnName("userId");

        builder.HasDiscriminator<int>("Id")
                .HasValue<AbstractRegistration>(0)
                .HasValue<Registration1>(1)
                .HasValue<Registration2>(2);
        builder.HasDiscriminator<string>("RegNumber")
                .HasValue<Registration1>("1");
    }
}

public class Registration1Config : IEntityTypeConfiguration<Registration1>
{
    public void Configure(EntityTypeBuilder<Registration1> builder)
    {
        // map table and schema
        builder.ToTable("tbl_reg1");

        // map properties with actual columns
        builder.Property(t => t.Id).HasColumnName("id");
        builder.Property(t => t.RegNumber).HasColumnName("reg_num");
    }
}

出现的错误是“实体类型'Registration1'是层次结构的一部分,但未配置鉴别属性”。

请提出建议

EntityFrameworkCore版本= 2.0.1.0

0 个答案:

没有答案