Ef Core TPH&关系问题

时间:2017-08-10 08:45:43

标签: asp.net-core foreign-keys entity-framework-core entity-relationship tph

我有这张桌子& class:

   public abstract class ProductBase
{
    public int Id { get; set; }
} 
public class Product:ProductBase
{

    public virtual ICollection<ProductType> Types { get; set; } = new HashSet<ProductType>();
    public virtual ICollection<OnlineStock> OnlineStocks { get; set; } = new HashSet<OnlineStock>();
}
public abstract class ProductTypeBase
{
    [Key]
    public int Id { get; set; }
    public int ProductId { get; set; }
    public int Quantity{ get; set; }
    [ForeignKey("ProductId")]
    protected virtual Product Product { get; set; }
}
public class ProductType:ProductTypeBase
{
    public new Product Product
    {
        get { return base.Product; }
        set { base.Product = value; }
    }
}
public class OnlineStock:ProductTypeBase
{
    public new Product Product
    {
        get { return base.Product; }
        set { base.Product = value; }
    }
    public virtual ICollection<ProductTypePicture> Pictures { get; set; } = new HashSet<ProductTypePicture>();

}

当我想添加带有producttype的新产品时。

Context.Porduct.Add(new Product
    {
     Types=new List<ProductTypes>
          {
             new ProductTypes {Quantity=8}
          }
    })

错误:&#34;无法投射类型&#39; ProductType&#39;键入&#39; OnlineStock&#39;。&#34;

我也有这个配置:

entityBuilder.Entity<Product>().HasMany(x=>x.Types).WithOne(x=>x.Product).HasForeignKey("ProductId");
        entityBuilder.Entity<Product>().HasMany(x=>x.OnlineStocks).WithOne(x=>x.Product).HasForeignKey("ProductId");

1 个答案:

答案 0 :(得分:0)

我解决了这个临时问题。

Shell "Magick Convert C:\Image.JPG C:\Image.PDF"