EF核心2.0,TPH模型类中的OwnsOne

时间:2018-01-20 17:06:46

标签: c# asp.net .net entity-framework

当我尝试在EF Core 2.0中迁移模型时遇到问题。

public class Profile
{
    [Key]
    public Guid Id { get; set; }

    public Guid UserId { get; set; }
    public ExternalUser User { get; set; }
}

public class OrganizationCustomerProfile : Profile
{
    public string CompanyName { get; set; }

    public Address LegalAddress { get; set; }
    public Address ActualAddress { get; set; }

    public BusinessRequisites Requisites { get; set; }

    public string President { get; set; }

    public IEnumerable<ContactPerson> ContactPerson { get; set; }
}

public class PersonCustomerProfile : Profile
{
    public FullName Person { get; set; }

    public Address Address { get; set; }
    public string PhoneNumber { get; set; }
}

public class ContactPerson
{
    [Key]
    public Guid Id { get; set; }

    public FullName Person { get; set; }

    public string Rank { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public Guid ProfileId { get; set; }
    public Profile Profile { get; set; }
}

在这里,我想添加复杂数据类型AddressBusinessRequisites,它们是:

public class BusinessRequisites
{
    public string OGRN { get; set; }
    public string INN { get; set; }
    public string KPPCode { get; set; }

    public string SettlementAccount { get; set; }

    public string RCBIC { get; set; }

    public string CorrespondentAccount { get; set; }

    public string BankName { get; set; }
}

public class Address
{
    public string FullAddress { get; set; }

    public float Latitude { get; set; } 
    public float Longtitude { get; set; }
}

我用于TPH绑定的代码:

public DbSet<Profile> UserProfiles { get; set; }
public DbSet<ContactPerson> ContactPerson { get; set; }
public DbSet<OrganizationCustomerProfile> OrganizationCustomerProfile { get; set; }

...

modelBuilder.Entity<Profile>().HasKey(u => u.Id);

modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.ActualAddress);
modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.LegalAddress);
modelBuilder.Entity<OrganizationCustomerProfile>().OwnsOne(e => e.Requisites);

但是当我尝试进行迁移时,出现错误:

  

“不能将表'UserProfiles'用于实体类型   'OrganizationCustomerProfile.ActualAddress#Address'因为它有一个   与派生实体类型“OrganizationCustomerProfile”的关系。   将关系指向基本类型“配置文件”或地图   'OrganizationCustomerProfile.ActualAddress#Address'到另一个   表“。

那么,这个错误的原因是什么?是否无法在EF Core 2.0中创建层次结构继承?

谢谢!

1 个答案:

答案 0 :(得分:1)