0

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

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; }   
}

使用 PostalAddressMap 类映射 PostalAddress 类

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<InvoicingAddress>”隐式转换为“System.Collections.Generic.ICollection<PostalAddress>”。存在显式转换(您是否缺少演员表?)

请问,你能帮我如何在 PostalAddress 子类和其他 TPT 类型之间设置外键吗?

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

4

1 回答 1

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; }

然后你可以使用你原来的映射。

后一种方法的缺点是,当你使用 eager 或 lazy loading 时,所有的PostalAddresses 都会被加载,你无法控制要加载哪种类型的地址。加载地址后,您可以按内存中的类型进行过滤:

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();

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

于 2012-10-29T15:56:19.003 回答