我正在尝试更改我的产品以使用标签而不是类别。实现标签非常容易,但是一旦我从产品模型中删除类别,我就会开始出错。
简单地说,我有以下课程:
public class BaseProduct
{
public int ID {get; set;}
public int CategoryID {get;set;}
public virtual Category Category {get;set;}
public virtual ICollection<ProductTag> Tags {get;set;}
}
[Table("Products")]
public class Product : BaseProduct
{
public string ImageUrl {get;set;}
}
[Table("Packages")]
public class Package : BaseProduct
{
public virtual ICollection<Product> Products {get;set}
}
public class Category
{
public int CategoryID {get;set;}
public string CategoryName {get;set;}
public int ParentID {get;set;}
public virtual Category Parent {get;set;}
public virtual ICollection Products {get;set;}
}
public class ProductTag
{
[Key, Column(Order = 0)]
public int ProductId {get;set;}
[Key, Column(Order = 1)]
public string TagName {get;set;}
public virtual BaseProduct Product {get;set;}
public virtual Tag Tag {get;set;}
}
public class Tag
{
[Key]
public string Name {get;set;}
public virtual Collection<ProductTag> ProductTags {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");
});
modelBuilder.Entity<Category>()
.HasOptional(c => c.Parent)
.WithMany(c => c.Children)
.HasForeignKey(c => c.ParentID);
modelBuilder.Entity<BaseProduct>()
.HasRequired(p => p.Category)
.WithMany(c => c.Products)
.HasForeignKey(p => p.CategoryID);
modelBuilder.Entity<ProductTag>()
.HasRequired(c => c.Product)
.WithMany(c => c.Tags)
.HasForeignKey(c => c.ProductId);
modelBuilder.Entity<ProductTag>()
.HasRequired(c => c.Tag)
.WithMany(c => c.ProductTags)
.HasForeignKey(c => c.TagName);
}
这一切都很好,我在我的数据库中得到了以下表格:
- 基础产品
- 类别
- 包关系
- 套餐
- 产品
- 产品标签
- 标签
但是,如果我编辑 BaseProduct 并删除类别 ID 和虚拟类别,如下所示:
public class BaseProduct
{
public int ID {get; set;}
public virtual ICollection<ProductTag> Tags {get;set;}
}
并从类别中删除虚拟产品:
public class Category
{
public int CategoryID {get;set;}
public string CategoryName {get;set;}
public int ParentID {get;set;}
public virtual Category Parent {get;set;}
}
并从 OnModelCreating 中删除映射:
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");
});
modelBuilder.Entity<Category>()
.HasOptional(c => c.Parent)
.WithMany(c => c.Children)
.HasForeignKey(c => c.ParentID);
modelBuilder.Entity<ProductTag>()
.HasRequired(c => c.Product)
.WithMany(c => c.Tags)
.HasForeignKey(c => c.ProductId);
modelBuilder.Entity<ProductTag>()
.HasRequired(c => c.Tag)
.WithMany(c => c.ProductTags)
.HasForeignKey(c => c.TagName);
}
然后,当我转到使用产品型号的页面时,我收到以下错误:
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或多个验证错误:
BaseProduct: : 在包含的 EntityContainer 中找不到 End 'BaseProduct' 的引用 EntitySet 'BaseProduct'。BaseProduct: : 在包含的 EntityContainer 中找不到 End 'BaseProduct' 的引用 EntitySet 'BaseProduct'