首先使用TPH在EF 4.3.1代码中定义外键

时间:2012-10-29 12:59:22

标签: c# .net entity-framework ef-code-first entity-framework-4.3

我创建了三个不同的类和一个存储不同类型地址的基类。 基类是与用户(附加当前地址)和邮政有关的邮政地址,邮政地址包含邮政编码和城市信息。

public class PostalAddress
{
    public virtual User User { get; set; }
    public DateTime LastUsed { get; private set; }

    public string OrientationNumber { get; set; }
    public int UserId { get; set; }

    public int PostId { get; set; }

    public int Id { get; set; }
    public virtual Post Post { get; private set; }

    public string Street  { get; set; }
}

public class Post
{
    public Post()
    {
        InvoicingAddresses = new List<InvoicingAddress>();
        ShippingAddresses = new List<ShippingAddress>();
        UserAddresses = new List<UserAddress>();
    }

    public virtual City City { get; set; }
    public virtual ICollection<InvoicingAddress> InvoicingAddresses { get; private set; }
    public virtual ICollection<ShippingAddress> ShippingAddresses { get; private set; }
    public virtual ICollection<UserAddress> UserAddresses { get; private set; }
    public int CityId { get; set; }
    public int Id { get; set; }
    public string ZipCode { get; set; }   
}

类PostalAddress使用类PostalAddressMap

进行映射
public class PostalAddressMap : EntityTypeConfiguration<PostalAddress>
{
    public PostalAddressMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        ToTable("PostalAddress");
        Property(t => t.Id).HasColumnName("Id");
        Property(t => t.LastUsed).HasColumnName("LastUsed").HasColumnType("datetime2");
        Property(t => t.OrientationNumber).HasColumnName("OrientationNumber");
        Property(t => t.UserId).HasColumnName("UserId");
        Property(t => t.PostId).HasColumnName("PostId");
        Property(t => t.Street).HasColumnName("Street");            

        // Relationships
        HasRequired(t => t.Post).WithMany(t => t.InvoicingAddresses).HasForeignKey(d => d.PostId);
        HasRequired(t => t.User)
            .WithMany(t => t.UserAddressess)
            .HasForeignKey(d => d.UserId);


    }
}

类InvoicingAddress,ShippingAddress和UserAddress使用Table per hierarchy方法从PostalAddress类继承。如果我想使用line

设置关系
        HasRequired(t => t.Post).WithMany(t => t.InvoicingAddresses).HasForeignKey(d => d.PostId);

我收到编译器错误无法隐式转换类型'System.Collections.Generic.ICollection&lt; InvoicingAddress&gt;'到'System.Collections.Generic.ICollection&lt; PostalAddress&gt;'。存在显式转换(您是否缺少演员?)

请帮助我如何在PostalAddress子类和其他TPT类型之间设置外键?

感谢您提供任何有用的答案。

1 个答案:

答案 0 :(得分:1)

您必须将PostIdPost属性从基类PostalAddress移至派生类InvoicingAddress等等。

public class InvoicingAddress : PostalAddress
{
    //...
    public int PostId { get; set; }
    public virtual Post Post { get; private set; }
}

...然后使用派生类的映射:

public class InvoicingAddressMap : EntityTypeConfiguration<InvoicingAddress>
{
    public InvoicingAddressMap()
    {
        HasRequired(t => t.Post)
            .WithMany(t => t.InvoicingAddresses)
            .HasForeignKey(d => d.PostId);
    }
}

或者您必须在Post中使用单个集合作为基类:

public virtual ICollection<PostalAddress> Addresses { get; private set; }

然后您可以使用原始映射。

后一种方法的缺点是,当您使用预先加载或延迟加载时,将加载所有PostalAddress es,并且您无法控制要加载的地址类型。加载地址后,您可以按内存类型进行过滤:

var invoicingAddresses = post.Addresses.OfType<InvoicingAddress>();

使用显式加载,您也可以过滤:

var post = context.Posts.Single(p => p.Id == 1);
context.Entry(post).Collection(p => p.Addresses).Query()
    .OfType<InvoicingAddress>().Load();

...仅使用Addresses es填充InvoicingAddress集合。