[英]Removing a column in EF Code First
我正在嘗試將產品更改為使用標簽而不是類別。 實施標簽非常容易,但是一旦我從產品模型中刪除了類別,我就會開始出錯。
簡而言之,我有以下課程:
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);
然后它又開始工作了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.