我正在尝试将我的产品更改为使用标签而不是类别。实施标签非常容易,但是一旦我从产品模型中删除了类别,我就会开始出错。
简而言之,我有以下课程:
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中找不到为终端'BaseProduct'引用的EntitySet'BaseProduct'。BaseProduct::在包含的EntityContainer中找不到最终“ BaseProduct”的引用EntitySet“ BaseProduct”
我想到了。我收到错误消息是因为删除了为BaseProduct构建模型的modelbuilder部分。我不知道这是必需的,但是我改变了
modelBuilder.Entity<ProductTag>()
.HasRequired(c => c.Product)
.WithMany(c => c.Tags)
.HasForeignKey(c => c.ProductId);
到
modelBuilder.Entity<BaseProduct>()
.HasMany(p => p.Tags)
.WithRequired(t => t.Product)
.HasForeignKey(t => t.ProductId);
然后它又开始工作了
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句