简体   繁体   English

EF6 CodeFirst继承和依赖于区分值的外部关系

[英]EF6 CodeFirst inheritance and foreign relation dependent on discriminator value

Currently I am writing an application using code first approach, and I need to create two very similar entities, lets say representing address so I've created: 当前,我正在使用代码优先方法编写应用程序,并且我需要创建两个非常相似的实体,可以说代表地址,所以我已经创建了:

public abstract class BaseEntity
{
    public Guid Id { set; get; }
}

public abstract class Address : BaseEntity
{
    public string Discriminator { set; get; }

    public Guid ObjectId { set; get; }

    public Guid CountryId { set; get; }

    public virtual Country Country { set; get; }

    public string Town { set; get; }

    public string PostalCode { set; get; }

    public string Street { set; get; }

    public string BuildingNumber { get; set; }

    public string FlatNumber { get; set; }

    public AddressType AddressType { get; set; }

    public bool IsDefault { get; set; }
}

And then I have two classes which inherits from Address class like so: 然后,我有两个继承自Address类的类,如下所示:

public class UserAddress : Address
{
    public virtual User User { set; get; }
}

public class ContractorAddress : Address
{
    public virtual Contractor Contractor { set; get; }
}

Now I need to make my model builder to map value of ObjectId property in abstract class to two different classes (for UserAddress it should points to User.Id value and User navigation property should use this value, for ContractorAddress it should points to Contractor.Id value and Contractor navigation property should use this value) I want to use Table per Hierarchy (TPH) approach. 现在,我需要使模型构建器将抽象类中的ObjectId属性的值映射到两个不同的类(对于UserAddress它应指向User.Id值,而User Navigation属性应使用此值,对于ContractorAddress它应指向Contractor.Id值和“ Contractor导航”属性应使用此值)我想使用“每个层次结构表(TPH)”方法。

My question is, how should I describe those relations in model builder? 我的问题是,我应该如何在模型构建器中描述这些关系?

Add configuration classes for your mappings. 为您的映射添加配置类。 For TPH use: 对于TPH使用:

class AddressConfiguration : EntityTypeConfiguration<Address>
{
    public AddressConfiguration()
    {
        Map<UserAddress>(m => m.Requires("AddressType").HasValue("user"));
        Map<ContractorAddress>(m => m.Requires("AddressType").HasValue("contractor"));
    }
}

As for the relations: 至于关系:

class UserAddressConfiguration : EntityTypeConfiguration<UserAddress>
{
    public UserAddressConfiguration()
    {
        HasRequired(m => m.User).WithMany().HasForeignKey(m => m.ObjectId);
    }
}
class ContractorAddressConfiguration : EntityTypeConfiguration<ContractorAddress>
{
    public ContractorAddressConfiguration()
    {
        HasRequired(m => m.Contractor).WithMany().HasForeignKey(m => m.ObjectId);
    }
}

Mapping your other properties should be pretty straight forward. 映射其他属性应该很简单。 Finally add your configurations to the model builder: 最后,将您的配置添加到模型构建器中:

modelBuilder.Configurations.Add(new AddressConfiguration());
modelBuilder.Configurations.Add(new UserAddressConfiguration());
modelBuilder.Configurations.Add(new ContractorAddressConfiguration());

Note that TPH means your entities are mapped to one table, so you have one column ObjectId referencing 2 different tables. 请注意,TPH表示您的实体已映射到一个表,因此您有一列ObjectId引用了2个不同的表。 I would recommend you remove ObjectId from your base class, add UserId and ContractorId to the subclasses and map your relations with those. 我建议您从基类中删除ObjectId,将UserId和ContractorId添加到子类中,并映射与它们的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM