简体   繁体   中英

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:

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:

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. 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:

BaseProduct: : The referenced EntitySet 'BaseProduct' for End 'BaseProduct' could not be found in the containing EntityContainer. BaseProduct: : The referenced EntitySet 'BaseProduct' for End 'BaseProduct' could not be found in the containing EntityContainer

I figured it out. I got the error because I removed the part of the modelbuilder that built the model for BaseProduct. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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