【问题标题】:EF core 2.0, OwnsOne in TPH model classesEF core 2.0,TPH 模型类中的OwnsOne
【发布时间】:2018-01-20 17:06:46
【问题描述】:

当我尝试在 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 不能创建层次继承吗?

谢谢!

【问题讨论】:

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


    【解决方案1】:

    【讨论】:

    猜你喜欢
    • 2018-08-28
    • 2018-05-01
    • 2023-02-16
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多