实体框架-如何向数据库添加复杂对象 [英] Entity framework - how to add complex objects to db

查看:54
本文介绍了实体框架-如何向数据库添加复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的MySQL数据库的简化模型(第一个模型): 我通过Entity Framework v6.1.3对其进行了映射.我有dataContext对象可以进行数据库查询.

Here is simplified model of my MySQL database (model first): I mapped it via Entity Framework v6.1.3. I have dataContext object to make database queries.

OrderProductsProduct1是用于建立外键连接的表,这是EF自动生成的类:

OrderProductsProduct1 is table to make foreign key connections, here is EF auto generated class:

 public partial class OrderProductsProduct1
 {
     public int ID { get; set; }
     public int OrderProductsID { get; set; }
     public int Product1ID { get; set; }
     public decimal Width { get; set; }

     public virtual OrderProducts OrderProducts { get; set; }
     public virtual Product1 Product1 { get; set; }
}

我很困惑如何向数据库添加新对象,该数据库具有两个一对多的外键.例如,如何正确地添加通过几个OrderProductsOrderProductsProduct1表连接的几个Product1对象的Order对象.

I'm confused how to add new objects to database, which has two one to many foreign keys. For example how to properly add Order object with few Product1 objects connected via OrderProducts and OrderProductsProduct1 tables.

到目前为止,我一直在尝试制作OrderProductsList,其中每个OrderProduct都有另一个ListOrderProductsProduct1 and 2.我在OrderProductsProduct1 and 2中添加了Products1 and 2.最终,OrderProductsorderProducts.Order = newOrder然后是dataContext.Add(newOrder)绑定到Order.但这会导致实体框架异常.

So far I was trying to make a List of OrderProducts, where each OrderProduct has another List with OrderProductsProduct1 and 2. To OrderProductsProduct1 and 2 I was adding Products1 and 2. Finally OrderProducts was bound to Order with orderProducts.Order = newOrder and then dataContext.Add(newOrder). But this causes Entity Framework exceptions.

我是否必须从最低级别(客户端,产品1,产品2)创建对象,然后使用dataContext.Add()将它们添加到数据库中,以获取其ID,然后将其与更高级别的对象(订单)绑定,依此类推,直到最高级别(OrderProductsProduct1和2)?这种方法省略了Entity Framework,并且在出现错误的情况下很难还原更改.

Do I have to create objects from the lowest level (Client, Product1, Product2), add them to database with dataContext.Add(), to get their ID's and then bound them with higher level objects (Order) and so on until the highest level (OrderProductsProduct1 and 2)? This approach omits Entity Framework and makes hard to revert changes in case of errors.

推荐答案

实体框架足够智能,可以查看对象的主键(ID)以查看它是否必须将其添加为新对象,或者仅使用对象的外键.

Entity Framework is smart enough to look at the primary Key (ID) of your objects to see if it must add it as a new object, or to use only the foreign key of the object.

假设您要添加一个具有现有OrderProducts和现有Product1的新OrderProductProducts1.代码可能如下:

Suppose you want to add a new OrderProductProducts1 with an existing OrderProducts and an existing Product1. Code could be like follows:

Product1 existingProduct = dbContext.Product1s...
OrderProducts existingorderProduct = dbContext.OrderProducts...

使用现有项目ID的方法:

Method where you use the ID of existing items:

OrderProductProducts1 addedProduct = dbContext.OderProductProducts1.Add(
    new OperProductProducts1()
    {
        // don't fill ID, will be filled during SaveChanges
        OrderProductsId = existingOrderProduct.ID,
        Product1Id = existingProduct.ID,
        ...
    });

除了填写现有项目的ID外,您还可以使用完整的现有项目:

Instead of filling the ID of the existing items you can also use the complete existing item:

OrderProductProducts1 addedProduct = dbContext.OderProductProducts1.Add(
    new OperProductProducts1()
    {
        // don't fill ID, will be filled during SaveChanges
        OrderProducts = existingOrderProduct,
        Product1 = existingProduct,
        ...
    });

Entity Framework足够聪明,可以检查现有项目的ID,以查看不必添加这些项目.

Entity Framework is smart enough to check the Id of the existing items to see they don't have to be added.

如果您的OrderProductProducts1不使用现有项目,而是现在使用的项目,只需将它们分配为现有项目即可:

If your OrderProductProducts1 does not use existing items but now ones, just assign them as if they were existing ones:

var nonExistingProduct = new Product1s() {...};
// note: because it does not exist ID == 0
OrderProductProducts1 addedProduct = dbContext.OderProductProducts1.Add(
    new OperProductProducts1()
    {
        // don't fill ID, will be filled during SaveChanges
        OrderProductsId = existingOrderProduct.Id,
        Product1 = nonExistingProduct,
        ...
    });

在SaveChanges期间,实体框架看到nonExistingProduct的ID为零,因此知道必须添加它.产品的ID将是外部Product1Id.

During SaveChanges Entity framework sees that nonExistingProduct has a zero ID, and thus knows that it has to be added. The Id of the product will be the foreign Product1Id.

如果需要,可以在添加OrderProductProducts1之前将新产品添加到DbContext中.但是,请记住,只要您没有保存更改,就不能使用ID:

If you want you can add the new product to the DbContext yourself before adding the OrderProductProducts1. However, remember as long as you didn't SaveChanges you can't use the ID:

var justAddedProduct = dbContext.Products.Add(new Product1s() {...});
// note: because it does not exist ID == 0
OrderProductProducts1 addedProduct = dbContext.OderProductProducts1.Add(
    new OperProductProducts1()
    {
        // don't fill ID, will be filled during SaveChanges
        OrderProductsId = existingOrderProduct.Id,
        Product1 = justAddedProduct,
        ...
    });

这篇关于实体框架-如何向数据库添加复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆