Entity Framework : When child entity is added, a duplicate parent entity is created instead of referencing existing entity?

.net asp.net c# entity-framework entity-framework-6

Question

enter image description here

A product can have multiple reviews. A review is made by a single customer. Therefore, review has both Customer and Product as properties.

Product.cs

namespace DatabaseProject.Models
{
    public class Product
    {
        public Product()
        {
            Reviews = new List < Review >();
        }

        public int Id { get; set; }
        public Catagory Catagory { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Specification { get; set; }
        public List<Review> Reviews { get; set; }
    }
}

Review.cs

namespace DatabaseProject.Models
{
    public class Review
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int Stars { get; set; }
        [Required]
        public Product Product { get; set; }
        [Required]
        public Customer Customer { get; set; }
    }
}

Customer.cs

namespace DatabaseProject.Models
{
    public class Customer
    {
        public Customer()
        {
            Addresses = new List<Address>();
            Reviews = new List<Review>();
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public List<Address> Addresses { get; set; }
        public List<Review> Reviews { get; set; }
    }
}

Method to add new review.

I add it to the reviews list in product table.

public bool AddReview(int id, Review review)
{
    using (var context = new ShopDbContext())
    {
        Product oldProduct = context.Products.Find(id);
        if (oldProduct == null)
        {
            return false;
        }
        oldProduct.Reviews.Add(review);


            context.SaveChanges();
            return true;
        }
    }

Adding a new Review

Here, since the review is added to product.Reviews I didn't have to pass the product property.

But I had to pass the customer. Somehow this creates a new customer rather than referencing the existing customer.

    productService.AddReview(1,
        new Review
        {
            Customer = customerService.Get(1),
            Stars = 2,
            Text = "It's a good camera",
        });

This causes a duplicate entry in Customers table.

enter image description here

1
0
11/10/2018 2:13:14 PM

Popular Answer

Your Review Model should have a CustomerID & Review Model should look like this:

namespace DatabaseProject.Models
{
    public class Review
    {
        public int Id { get; set; }
        [Required]
        public int CustomerId { get; set; }
        [Required]
        public int ProductId { get; set; }
        public string Text { get; set; }
        public int Stars { get; set; }

        [ForeignKey("ProductId")]
        public Product Product { get; set; }

        [ForeignKey("CustomerId")]
        public Customer Customer { get; set; }
    }
}

And you must add a new review like this:

productService.AddReview(1,
    new Review
    {
        CustomerId = 1,
        Stars = 2,
        Text = "It's a good camera",
        ProductId = 1
    })

In present code you are passing an object of Customer Model in DbSet.Add method which adds a new entity to a context

1
11/10/2018 2:41:08 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow