简体   繁体   English

EF Code首先创建过时的列吗?

[英]EF Code First creating obsolete column?

I have the following classes that I use for my database: 我有以下用于数据库的类:

public abstract class BaseProduct
{
    public int ID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string ProductName { get; set; }

    [Required, StringLength(10000), Display(Name = "Product Description"), DataType(DataType.MultilineText)]
    public string Description { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "Price")]
    public double? UnitPrice { get; set; }

    public int? CategoryID { get; set; }

    public virtual Category Category { get; set; }

    public string Author { get; set; }

    public virtual ICollection<PriceRelation> Prices { get; set; }
}
[Table("Packages")]
public class Package : BaseProduct
{
    public virtual ICollection<Product> Products { get; set; }
}
[Table("Products")]
public class Product : BaseProduct
{
    public virtual ICollection<Package> Packages { get; set; }
}
public class Category
{
    [ScaffoldColumn(false)]
    public int CategoryId { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string CategoryName { get; set; }

    [Display(Name = "Category Description")]
    public string Description { get; set; }

    public int? ParentID { get; set; }
    [ForeignKey("ParentID")]
    public virtual Category Parent { get; set; }

    public virtual ICollection<Category> Children { get; set; } 

    public virtual ICollection<Product> Products { get; set; }

}

And this is the model builder: 这是模型构建器:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Package>().HasMany(p => p.Products).WithMany(p => p.Packages)
            .Map(m =>
                {
                    m.ToTable("PackageRelations");
                    m.MapLeftKey("PackageID");
                    m.MapRightKey("ProductID");
                });
    }
    }

It sets up the relations as I want to, in the following way: 它通过以下方式设置我想要的关系:

Table: BaseProducts Columns: ID, ProductName, Description, ImagePath, UnitPrice, CategoryID, Author 表:BaseProducts列:ID,产品名称,描述,ImagePath,单价,类别ID,作者

Table: Packages Columns: ID 表:软件包列:ID

Table: Products Columns: ID, Category_CategoryID 表:产品列:ID,Category_CategoryID

The thing I'm wondering is, why does it create the column Category_CategoryID in the products table? 我想知道的是,为什么要在产品表中创建Category_CategoryID列? When I populate the table, all the values in this column are null, so it doesn't look like it's being used for anything. 当我填充表格时,此列中的所有值均为null,因此看起来好像没有用于任何东西。

Also, it doesn't seem to have the relations right - because the virtual collection of Products on the category is always empty. 而且,它似乎没有正确的关系-因为该类别上的Products的虚拟集合始终为空。

您可能需要将CategoryID重命名为CategoryId,以便EF可以将其识别为Category实体的关键字段。

I figured it out! 我想到了! The virtual ICollection in the category was of the type Product, when in fact it should be BaseProduct. 类别中的虚拟ICollection的类型为Product,而实际上应该为BaseProduct。

It makes sense that the Product would get an extra column to reference the Category when it has that reference for only that type. 当产品仅具有针对该类型的引用时,该产品将获得一个额外的列来引用类别是有意义的。

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

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