删除EF Code First中的列

时间:2013-08-23 10:28:03

标签: c# asp.net-mvc entity-framework

我正在尝试将我的产品更改为使用代码而不是类别。实现标签非常简单,但只要从产品模型中删除类别,我就会开始出错。

简单地说,我有以下几个类:

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

这一切都运行正常,我在我的数据库中得到以下表格:

  • BaseProducts
  • 分类
  • PackageRelations
  • 产品
  • ProductTags
  • 标签

但是,如果我编辑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'

1 个答案:

答案 0 :(得分:0)

我明白了。我收到了错误,因为我删除了为BaseProduct构建模型的模型构建器部分。我不知道它是必需的,但我改变了

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

然后再次运作