Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF代码首先创建过时列?_C#_Asp.net_Entity Framework_Ef Code First - Fatal编程技术网

C# EF代码首先创建过时列?

C# EF代码首先创建过时列?,c#,asp.net,entity-framework,ef-code-first,C#,Asp.net,Entity Framework,Ef Code First,我有以下用于数据库的类: 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 Descr

我有以下用于数据库的类:

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

}
公共抽象类BaseProduct
{
公共int ID{get;set;}
[必需,字符串长度(100),显示(Name=“Name”)]
公共字符串ProductName{get;set;}
[必需,StringLength(10000),Display(Name=“Product Description”),数据类型(DataType.multilitext)]
公共字符串说明{get;set;}
公共字符串ImagePath{get;set;}
[显示(Name=“价格”)]
公共双价?单价{get;set;}
公共int?CategoryID{get;set;}
公共虚拟类别{get;set;}
公共字符串作者{get;set;}
公共虚拟ICollection价格{get;set;}
}
[表(“包装”)]
公共类包:BaseProduct
{
公共虚拟ICollection产品{get;set;}
}
[表(“产品”)]
公共类产品:BaseProduct
{
公共虚拟ICollection包{get;set;}
}
公共类类别
{
[脚手架立柱(假)]
public int CategoryId{get;set;}
[必需,字符串长度(100),显示(Name=“Name”)]
公共字符串CategoryName{get;set;}
[显示(名称=“类别说明”)]
公共字符串说明{get;set;}
public int?ParentID{get;set;}
[ForeignKey(“ParentID”)]
公共虚拟类别父项{get;set;}
公共虚拟ICollection子项{get;set;}
公共虚拟ICollection产品{get;set;}
}
这是模型生成器:

    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");
                });
    }
    }
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasMany(p=>p.Products)。WithMany(p=>p.Packages)
.Map(m=>
{
m、 ToTable(“包装关系”);
m、 MapLeftKey(“PackageID”);
m、 MapRightKey(“产品ID”);
});
}
}
它以以下方式建立我想要的关系:

表:基本产品 列:ID、ProductName、描述、ImagePath、单价、类别ID、作者

表:包裹 列:ID

表:产品 列:ID、类别\类别ID

我想知道的是,为什么它要在products表中创建Category_CategoryID列?当我填充该表时,该列中的所有值都为null,因此看起来它没有被用于任何用途


此外,它似乎没有正确的关系-因为类别上的虚拟产品集合总是空的。

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

我找到了它!类别中的虚拟ICollection是Product类型,而实际上它应该是BaseProduct


当产品仅对该类型具有引用时,它将获得一个额外的列来引用该类别,这是有道理的。

这并没有解决它。我在原来的帖子中添加了更多的信息,因为我发现了这个问题的另一个副作用。我建议使用另一个名为“PackageRelation”的concret类,并向类添加引用,而不是依赖EF。当然,如果有可能的话。根据经验,我发现多对多映射并非在所有情况下都能很好地工作,如果您想添加排序等,添加一个单独的类可以提供更大的灵活性。这是我使用的第一种方法,但它不能让我轻松访问相关对象(具有延迟加载的虚拟属性)。我看不出排序有多容易,你能举个例子吗?假设你需要按特定顺序显示包装的产品。当使用自动生成的相交表时,无法实现这一点。对于名为“PackageRelations”的新类,您只需添加一个名为“SortOrder”的新字段,即可为您解决该问题。