Entity Framework - Fluent APi - Create table with 2 FK

别来无恙 提交于 2019-12-30 14:09:34

问题


I have this product class which has a list of products associated.

eg:

Product = StarWar

AssociatedProducts = Episode V: The Empire Strikes Back, Episode VI: Return of the Jedi, Episode VII: The Force Awakens

But EF generates the database with some extra columns.

This is my Product Class:

public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string FullDescription { get; set; }
    public decimal UnitPrice { get; set; }
    ......
    public virtual ICollection<AssociatedProducts> AssociatedProducts { get; set; }
    ......
}

This is my AssociatedProduct Class:

public class AssociatedProducts
{
    public int Id { get; set; }

    public int ProductId { get; set; }
    public int AssociatedProductId { get; set; }

    public int DisplayOrder { get; set; }
    public DateTime CreatedOn { get; set; }

    public virtual Product Products { get; set; }
    public virtual Product AssociatedProductsId { get; set; }
}

This is my mapping for AssocietedProduct class:

public AssociatedProductsMap()
    {
        // Primary Key
        HasKey(a => a.Id);

        // Properties
        Property(a => a.CreatedOn).IsRequired();
        Property(a => a.ProductId).IsRequired();
        Property(a => a.AssociatedProductId).IsRequired();
        Property(a => a.DisplayOrder).IsRequired();

        //Relationship
        HasRequired(a => a.Products)
            .WithMany(p => p.Products)
            .HasForeignKey(a => a.ProductId)
            .WillCascadeOnDelete(false);

        HasRequired(a => a.AssociatedProductsId)
            .WithMany(p => p.AssociatedProducts)
            .HasForeignKey(a => a.AssociatedProductId)
            .WillCascadeOnDelete(false);

        //Table  
        ToTable("AssociatedProducts");
    }

And this is how I am seeding:

var associetedProducts = new List<AssociatedProducts>
        {
            new AssociatedProducts {ProductId= 1, AssociatedProductId = 3, DisplayOrder = 1, CreatedOn = DateTime.Now},
            new AssociatedProducts {ProductId= 1, AssociatedProductId = 4, DisplayOrder = 2, CreatedOn = DateTime.Now},
            new AssociatedProducts {ProductId= 1, AssociatedProductId = 5, DisplayOrder = 3, CreatedOn = DateTime.Now}
        }

new List<Product>
        {
          new Product {Name = "StarWar", ShortDescription = "...", FullDescription = "P.......", UnitPrice = 15m, AssociatedProducts = associetedProducts},
          new Product {Name = "StarWar Episode V", ShortDescription = "...", FullDescription = "P.......", UnitPrice = 15m},
          new Product {Name = "StarWar Episode VI", ShortDescription = "...", FullDescription = "P.......", UnitPrice = 15m},
          new Product {Name = "StarWar Episode VII", ShortDescription = "...", FullDescription = "P.......", UnitPrice = 15m},
    }.ForEach(a => context.Products.AddOrUpdate(a));

context.SaveChanges();

This is what I expect from AssociatedProductsId Table: ID + ProductId (FK) + AssociatedProductsId(FK) + date + ......

But this is what I get:

What is my mistake ? Is this a good way to associate a product ?


回答1:


I think your classes are not right.

1 Problem: Why EF generates the table with extra columns?

In that case EF is using TPH strategy. Take a look at this link http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx

2 Problem: What is my mistake ? Is this a good way to associate a product ?

Your mapping should be something like this:

public class Product
{
    public int ProductId { get; set; }

    public string Name { get; set; }

    public virtual ICollection<ProductAssociation> AssociatedProducts { get; set; }

    public virtual ICollection<ProductAssociation> ProductsAssociatedThisProduct { get; set; }
}

public class ProductAssociation
{
    public int ProductId { get; set; }

    public int ProductAssociatedId { get; set; }

    public DateTime CreationDate { get; set; }

    public virtual Product Product { get; set; }

    public virtual Product ProductAssociated { get; set; }
}

public class Context : DbContext
{
    public Context() : base("Model2")
    {

    }

    public DbSet<Product> Products { get; set; }
    public DbSet<ProductAssociation> ProductsAssociations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .HasKey(i => i.ProductId);

        modelBuilder.Entity<ProductAssociation>()
            .HasKey(i => new {i.ProductId, i.ProductAssociatedId });

        modelBuilder.Entity<Product>()
            .HasMany(i => i.AssociatedProducts)
            .WithRequired(i => i.ProductAssociated)
            .HasForeignKey(i => i.ProductAssociatedId)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Product>()
            .HasMany(i => i.ProductsAssociatedThisProduct)
            .WithRequired(i => i.Product)
            .HasForeignKey(i => i.ProductId)
            .WillCascadeOnDelete(false);


        base.OnModelCreating(modelBuilder);
    }


}

Seed

var product1 = new Product() { Name = "StarWars", ProductId = 1 };

var product2 = new Product() { Name = "StarWars II", ProductId = 2 };

context.ProductsAssociations.AddOrUpdate(new ProductAssociation { Product = product1, CreationDate = DateTime.Now, ProductAssociated = product2 });
context.Products.AddOrUpdate(product1);
context.Products.AddOrUpdate(product2);
context.SaveChanges();


来源:https://stackoverflow.com/questions/31461173/entity-framework-fluent-api-create-table-with-2-fk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!