简体   繁体   English

首先在EF代码中删除列

[英]Removing a column in EF Code First

I am trying to change my products to use tags instead of categories. 我正在尝试将产品更改为使用标签而不是类别。 Implementing the tags was quite easy, but as soon as I remove the category from the product model, I'm starting to get errors. 实施标签非常容易,但是一旦我从产品模型中删除了类别,我就会开始出错。

Simply put I have the following classes: 简而言之,我有以下课程:

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

I am doing this on model creating: 我正在创建模型时这样做:

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

This is all working fine, and I get the following tables in my DB: 一切正常,我在数据库中获得了以下表格:

  • BaseProducts 基础产品
  • Categories 分类目录
  • PackageRelations 包关系
  • Packages 配套
  • Products 产品展示
  • ProductTags 产品标签
  • Tags 标签

But if I edit BaseProduct and remove category id and the virtual category, like this: 但是,如果我编辑BaseProduct并删除类别ID和虚拟类别,如下所示:

public class BaseProduct
{
    public int ID {get; set;}
    public virtual ICollection<ProductTag> Tags {get;set;}
}

And remove the virtual products from the category: 并从以下类别中删除虚拟产品:

public class Category
{
    public int CategoryID {get;set;}
    public string CategoryName {get;set;}
    public int ParentID {get;set;}
    public virtual Category Parent {get;set;}
}

And remove the mappings from OnModelCreating: 并从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);
}

Then, when I go to a page that uses the product model, I am getting the following error: 然后,当我转到使用产品模型的页面时,出现以下错误:

Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: 异常详细信息:System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或多个验证错误:

BaseProduct: : The referenced EntitySet 'BaseProduct' for End 'BaseProduct' could not be found in the containing EntityContainer. BaseProduct::在包含的EntityContainer中找不到最终“ BaseProduct”的引用EntitySet“ BaseProduct”。 BaseProduct: : The referenced EntitySet 'BaseProduct' for End 'BaseProduct' could not be found in the containing EntityContainer BaseProduct::在包含的EntityContainer中找不到最终“ BaseProduct”的引用EntitySet“ BaseProduct”

I figured it out. 我想到了。 I got the error because I removed the part of the modelbuilder that built the model for BaseProduct. 我收到错误消息是因为删除了为BaseProduct构建模型的modelbuilder部分。 I did not know it was required, but I changed 我不知道这是必需的,但是我改变了

modelBuilder.Entity<ProductTag>()
            .HasRequired(c => c.Product)
            .WithMany(c => c.Tags)
            .HasForeignKey(c => c.ProductId);

to

modelBuilder.Entity<BaseProduct>()
            .HasMany(p => p.Tags)
            .WithRequired(t => t.Product)
            .HasForeignKey(t => t.ProductId);

and then it was working again 然后它又开始工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM