Abbas
Abbas

Reputation: 5044

Adding records in multiple table using EF6 code first

I am using EF6 code first approach, i have 3 tables:

  1. Product
  2. PromotionalOffers
  3. PromotionalOfferProduct

I have few products already added inside the products table, now i want that on adding new promotional offer, i should be able to link some products (more than 1) to promotionalofferproducts, but in my case it adds new product

here are my Entities

Product

 public class Product 
        {
            [Key]
            public long ProductId { get; set; }
            [Required, StringLength(100)]
            public string Name { get; set; }
    }

PromotionalOffer

public class PromotionalOffer
    {
        [Key]
        public long PromotionalOfferID { get; set; }
        [Required]
        [StringLength(100)]
        public string Name { get; set; }
        public virtual List<Product> Products { get; set; }
        public void Add()
        {
            db.PromotionalOffers.Add(this);
            db.SaveChanges();
        }
}

The 3rd table was automatically created by EF based on List<Product> products in PromotionalOffer class.

And below is my client code:

IPromotionalOffer pOffer = Factory.Instance.Create<IPromotionalOffer>();
            Domain.Product p = new Domain.Product
            {
                ProductId = 1,
                Name = "Colgate",
            };
            pOffer.Name = "Holi";
            pOffer.Products.Add(p);
            pOffer.Add();

Though this adds entry in PromotionalOfferProducts it also creates entry in Products table (though it should not add in Product table).

Upvotes: 0

Views: 53

Answers (1)

Gerrie Pretorius
Gerrie Pretorius

Reputation: 4131

The Problem is you are adding a new product because you create a new instance of Product, instead of using an existing one.

So to solve this, Load your product with entityframework, instead of instantiating your own instance of product.

so in general entityframework terms if you did not use dependecy injection container something like this:

var loadedProduct = dbContext.Product.Find(yourProductId);
IPromotionalOffer pOffer = Factory.Instance.Create<IPromotionalOffer>();
            pOffer.Name = "Holi";
            pOffer.Products.Add(loadedProduct);
            pOffer.Add();

the reason is, your entity classes is not actually used by entityframework directly, entityframework inherits from your product class and make its own class that it can add more properties to, in order to track the entity. If you create a new instance yourself, entityframework cant track it, and thus assumes its a new product, instead of an existing one, since it is tracking the existing ones that are being loaded.

More ideally i think you need to change your entity mappings so that you can do this the other way around, thus load your product from entity framework, and then add a promotionalOffer to your product, instead of adding a product to your promotional offer.

Upvotes: 2

Related Questions