Entity Framework : When child entity is added, a duplicate parent entity is created instead of referencing...
up vote
0
down vote
favorite
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.
c# asp.net .net entity-framework entity-framework-6
add a comment |
up vote
0
down vote
favorite
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.
c# asp.net .net entity-framework entity-framework-6
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
c# asp.net .net entity-framework entity-framework-6
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.
c# asp.net .net entity-framework entity-framework-6
c# asp.net .net entity-framework entity-framework-6
edited Nov 10 at 14:13
asked Nov 10 at 13:52
Enzio
130110
130110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
I think you need a CustomerId propery in your Reviews table, and pass the customerId when adding a new review.
public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int CustomerId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
productService.AddReview(1,
new Review
{
CustomerId = 1,
ProductId = XXX,
Stars = 2,
Text = "It's a good camera",
})
Then, you would need to create a foreign key between ProductId and Product table, and CustomerId and Customer table.
In this way, you wouldn't need to load the customer/product when addign a new review. You will only need the identifiers.
New contributor
add a comment |
up vote
0
down vote
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
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I think you need a CustomerId propery in your Reviews table, and pass the customerId when adding a new review.
public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int CustomerId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
productService.AddReview(1,
new Review
{
CustomerId = 1,
ProductId = XXX,
Stars = 2,
Text = "It's a good camera",
})
Then, you would need to create a foreign key between ProductId and Product table, and CustomerId and Customer table.
In this way, you wouldn't need to load the customer/product when addign a new review. You will only need the identifiers.
New contributor
add a comment |
up vote
1
down vote
I think you need a CustomerId propery in your Reviews table, and pass the customerId when adding a new review.
public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int CustomerId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
productService.AddReview(1,
new Review
{
CustomerId = 1,
ProductId = XXX,
Stars = 2,
Text = "It's a good camera",
})
Then, you would need to create a foreign key between ProductId and Product table, and CustomerId and Customer table.
In this way, you wouldn't need to load the customer/product when addign a new review. You will only need the identifiers.
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
I think you need a CustomerId propery in your Reviews table, and pass the customerId when adding a new review.
public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int CustomerId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
productService.AddReview(1,
new Review
{
CustomerId = 1,
ProductId = XXX,
Stars = 2,
Text = "It's a good camera",
})
Then, you would need to create a foreign key between ProductId and Product table, and CustomerId and Customer table.
In this way, you wouldn't need to load the customer/product when addign a new review. You will only need the identifiers.
New contributor
I think you need a CustomerId propery in your Reviews table, and pass the customerId when adding a new review.
public class Review
{
public int Id { get; set; }
public string Text { get; set; }
public int Stars { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int CustomerId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[ForeignKey("CustomerId")]
public Customer Customer { get; set; }
}
productService.AddReview(1,
new Review
{
CustomerId = 1,
ProductId = XXX,
Stars = 2,
Text = "It's a good camera",
})
Then, you would need to create a foreign key between ProductId and Product table, and CustomerId and Customer table.
In this way, you wouldn't need to load the customer/product when addign a new review. You will only need the identifiers.
New contributor
edited Nov 10 at 14:36
New contributor
answered Nov 10 at 14:11
Gabitu
864
864
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
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
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
edited Nov 10 at 14:41
answered Nov 10 at 14:22
Muhammad Saqlain
2,19221934
2,19221934
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239648%2fentity-framework-when-child-entity-is-added-a-duplicate-parent-entity-is-crea%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password