Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
758 views
in Technique[技术] by (71.8m points)

asp.net mvc - Entity Framework database mapping relationships (Duplicate creation using Seed() method)

I created a post with an issue and another issue. These can be looked at for references but i consider them as handled.

My question arising from these issues and the action i (need or not need) to apply bothers me because i don't quite understand EF its behavior and expectations.

I have a Product, PurchasePrice and SalesPrice entity where my initial thought was that 1 Product can have multiple PurchasePrices but that 1 PurchasePrice only can exist in 1 Product (same for SalesPrice).

Therefore these relations:

// NOTE that BaseEntity just has a "int ID" prop and datetimes/stamps
public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }
}

public  class PurchasePrice:BaseEntity
{      
   public Product Product { get; set; }
}

public  class SalesPrice:BaseEntity
{      
   public Product Product { get; set; }
}

Now, lets add a Supplier Entity to it because that is why i seperate Sales & Purchase apart and don't create an Enum out of it, because 1 Product (in database) can have multiple suppliers, each having their own Sales/Purchase prices AND another Productnumber value.

So above becomes:

public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }
   // added
   public  ICollection<Supplier> Suppliers { get; set; }
}

public  class PurchasePrice:BaseEntity
{      
   public Product Product { get; set; }
   // added
   public Supplier Supplier { get; set; }
}

public  class SalesPrice:BaseEntity
{      
   public Product Product { get; set; }
   // added
   public Supplier Supplier { get; set; }
}

// added Entity Supplier into the party
public class Supplier : BaseEntity
{
    public ICollection<Product> Products { get; set; }
    public ICollection<PurchasePrice> PurchasePrices { get; set; }
    public ICollection<SalesPrice> SalesPrices { get; set; }
}

Lets continue a little furhter because it doesn't stop there, i want to keep track of these Product-Supplier-Prices relations so i created a Entity called 'ProductSupplierForContract' which would have the following structure:

public class ProductSupplierForContract:BaseEntity
{
    public string ProductnumberValue { get; set; }

    public int Product_Id { get; set; }
    public int Supplier_Id { get; set; }
    public int? Contract_Id { get; set; }

    public virtual Product Product { get; set; }
    public virtual Supplier Supplier { get; set; }
    public virtual Contract Contract { get; set; }
}

Finally i have a Contract Entity which has the following structure:

public class Contract:BaseEntity
{
  [Required]
  public ICollection<Product> Products { get; set; }
  public ICollection<ProductSupplierForContract> ProductSupplierForContracts { get; set; }
}

So Product becomes:

public class Product : BaseEntity
{
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<PurchasePrice> SalesPrices { get; set; }    
   public  ICollection<Supplier> Suppliers { get; set; }
   // added
   public  ICollection<Contract> Contracts { get; set; }
}   

Custom Seeding (inherits from DropCreateDatabaseAlways):

protected override void Seed(ApplicationDbContext context)
{
   PurchasePrice purchaseprice = new PurchasePrice((decimal)17.70);
   ctx.PurchasePrices.Add(purchaseprice);

   Product product1 = new Product("test product 1",purchaseprice);
   ctx.Products.Add(product1);

   base.Seed(ctx);
}

I also have mappings defined in Fluent API:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
         // setting the Product FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasRequired(psfc => psfc.Product)
                                                            .WithMany(p => p.ProductSupplierForContracts)
                                                            .HasForeignKey(psfc => psfc.Product_Id);

        // setting the Supplier FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasRequired(psfc => psfc.Supplier)
                                                           .WithMany(s => s.ProductSupplierForContracts)
                                                           .HasForeignKey(psfc => psfc.Supplier_Id);

        // setting the Contract FK relation required + related entity
        modelBuilder.Entity<Entity.ProductSupplierForContract>().HasOptional(psfc => psfc.Contract)
                                                          .WithMany(c => c.ProductSupplierForContracts)
                                                          .HasForeignKey(psfc => psfc.Contract_Id);

    }

Now, initially i didn't had any issues and i really really don't understand what has brought up this sudden change that i now got duplicates Products when i seed my database. I can strip it down to just adding a simple PurchasePrice with a value and a Product having a reference to this PurchasePrice and there is my duplicate.

Changing the relation inside the PurchasePrice class of the Entity Product, to a ICollection doesn't create a duplicate but i don't want this collection because it is not a Many to Many relation ...

I have tried enormous amounts of things but nothing that "resolved" this (if this is a problem to start with, for me yes but maybe not for EF) like removing inhertance BaseEntity, changinge Mapping (Fluent AND annotations), changed the way i seeded and initialized everthing, defining ID's myself, you name it ...

Mind that the purpose is not to optimize the way i seed in anyway but to have a decent working Model AND to understand what EF does and what it wants.

My questions:

  • Why is this duplicate occuring/appearing ?
  • If i want to be able to have 1 instance holding the relation of Price-Supplier-Product-Contract, how should i do this? Answer is here
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I fixed my problem by redesigning the model. I have added a additional Entity ProductForSupplier which holds the relation of a Product & Supplier and a Productnumber.

public class ProductForSupplier:BaseEntity
{
    public string ProductnumberValue { get; set; }

    [Required]
    public Product Product { get; set; }

    [Required]
    public Supplier Supplier { get; set; }
}

Added a Entity ProductsForContract which will hold the amount of a Product-Supplier relation for 1 contract:

public class ProductsForContract
{
    public int ProductsForContractId { get; set; }        
    public int Amount { get; set; }
    public ProductForSupplier ProductForSupplier { get; set; }
    public Contract Contract { get; set; }
}

And the Existing Entity ProductSupplierForContract becomes:

public class ProductSupplierForContract:BaseEntity
{
    public ICollection<ProductsForContract> ProductsForContract { get; set; }

    [Required]
    public Contract Contract { get; set; }
}

This gives me the flexibility to keep relations of any kind between the entities and also has taken care of the duplicate (which i still don't know the cause of).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...