实体框架:添加子实体后,会创建重复的父实体,而不是引用现有实体吗?

时间:2018-11-10 13:52:34

标签: c# asp.net .net entity-framework entity-framework-6

enter image description here

一个产品可以有多个评论。审核由单个客户进行。 因此,审阅将“客户”和“产品”都作为属性。

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; }
    }
}

添加新评论的方法。

我将其添加到产品表的评论列表中。

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;
        }
    }

添加新评论

在这里,因为评论已添加到产品。评论我不必传递产品属性。

但是我必须通过客户。这样就创建了一个新客户,而不是引用现有客户。

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

这会在“客户”表中导致重复的条目。

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为您需要“评论”表中的 CustomerId 属性,并在添加新评论时通过 customerId

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",
    })

然后,您需要在 ProductId Product 表以及 CustomerId Customer 表格。

这样,您在添加新评论时就无需加载客户/产品。您只需要标识符。

答案 1 :(得分:1)

您的评论模型应具有 CustomerID ,并且评论模型应如下所示:

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; }
    }
}

并且您必须添加这样的新评论:

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

在当前代码中,您正在DbSet.Add method中传递客户模型的对象,该对象将新实体添加到上下文中