简体   繁体   English

实体框架6多对多

[英]Entity Framework 6 Many to many

I'm trying to learn to use Entity Framework and am working through the Entity Framework Recipes 6 Book (not even going to try and hide that). 我正在尝试学习使用Entity Framework,并且正在研究Entity Framework Recipes 6 Book(甚至不会尝试隐藏它)。
Working on 2-4: 在2-4上工作:
3 tables: 3张桌子:
Order: OrderID, OrderDate, (Nav Properties OrderItems) 订单:OrderID,OrderDate(导航属性OrderItems)
OrderItem: OrderId, SKU, Count (Nav Properties, Item, Order) OrderItem:OrderId,SKU,计数(导航属性,项目,订单)
Item: SKU, Description, Price (Nav Properties, OrderItems) 项目:SKU,描述,价格(导航属性,OrderItems)

using (var context = new EFRecipesContext()) {
    var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
    var item = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
    var oi = new OrderItem { Order = order, Item = item, Count = 1 };
    item = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
    oi = new OrderItem { Order = order, Item = item, Count = 3 };
    item = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
    oi = new OrderItem { Order = order, Item = item, Count = 1 };
    context.Orders.Add(order);
    context.SaveChanges();
}

The only thing that gets added to the database is the order and nothing else, none of the items, etc... I had a problem with the previous example and had to add the "other" items individually but I thought that point was that you could just do the one "Add" and it would add all the objects to the Database? 唯一添加到数据库的是顺序,没有别的,没有项目,等等...我在前面的示例中遇到了问题,不得不单独添加“其他”项目,但我认为这一点是您可以只做一个“添加”,它将所有对象添加到数据库中?

Thanks in advance! 提前致谢!

UPDATE UPDATE
Ok I made the following changes based on the suggestions below and now it's working 好的,我根据以下建议进行了以下更改,现在可以正常工作了

using (var context = new EFRecipesContext()) {
            var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
            var item = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
            var oi = new OrderItem { Order = order, Item = item, Count = 1 };
            order.OrderItems.Add(oi);   // suggestion from Stackoverflow.

            item = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
            oi = new OrderItem { Order = order, Item = item, Count = 3 };
            order.OrderItems.Add(oi);   // suggestion from Stackoverflow.

            item = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
            oi = new OrderItem { Order = order, Item = item, Count = 1 };
            order.OrderItems.Add(oi);   // suggestion from Stackoverflow.

            context.Orders.Add(order);
            context.SaveChanges();
        }

At least now I know what to change in their code going forward to get it working. 至少现在我知道他们将在代码中进行哪些更改以使其正常工作。 Thanks!! 谢谢!!

You never actually add the OrderItems to the DbContext collection. 您实际上从未将OrderItems添加到DbContext集合中。

There is also a problem with your code, which means you are overwriting your items and oi variable values multiple times. 您的代码也存在问题,这意味着您要多次覆盖项目和oi变量值。

The below code makes some assumptions, since you haven't supplied more of your code, but should be easy to adapt. 下面的代码做了一些假设,因为您没有提供更多的代码,但是应该很容易适应。

using (var context = new EFRecipesContext()) {
    var order = new Order { OrderId = 1, OrderDate = new DateTime(2010, 1, 18) };
    context.Orders.Add(order);
    var item1 = new Item { SKU = 1729, Description = "Backpack", Price = 29.97M };
    var oi1 = new OrderItem { Order = order, Item = item, Count = 1 };
    context.OrderItems.Add(oi1);

    var item2 = new Item { SKU = 2929, Description = "Water Filter", Price = 13.97M };
    var oi2 = new OrderItem { Order = order, Item = item, Count = 3 };
    context.OrderItems.Add(oi2);

    var item3 = new Item { SKU = 1847, Description = "Camp Stove", Price = 43.99M };
    var oi3 = new OrderItem { Order = order, Item = item, Count = 1 };
    context.OrderItems.Add(oi3);

    context.SaveChanges();
}

Unless you are using the variables again, you could do the definition of the objects inline like this aswell, which is slightly less repetitive: 除非再次使用变量,否则还可以像下面这样内联地定义对象,这将减少重复性:

    context.OrderItems.Add(new OrderItem { 
        Order = order, 
        Item = new Item { 
            SKU = 1729, 
            Description = "Backpack", 
            Price = 29.97M 
        }, 
        Count = 1 
    });

This is not really about many to many, but about the way how EF discovers related data. 这实际上并不是很多,而是关于EF如何发现相关数据的方式。

You need to explicitely add the entry to the database, that is actually referencing the related data. 您需要明确地将条目添加到数据库,即实际上是在引用相关数据。 In your case, this is oi , so if you add it 就您而言,这是oi ,所以如果您添加它

context.OrderItems.Add(oi);
context.SaveChanges();

the related io.Order and io.Item will be considered by EF. EF将考虑相关的io.Orderio.Item But EF has no way of knowing about this relation when you only provide order without providing navigation information from order to io . 但是,当您仅提供order而不提供从orderio导航信息时,EF无法了解这种关系。

Another way of solving the issue (instead of adding io explicitely) would be to update order.OrderItems before saving: 解决该问题的另一种方法(而不是显式添加io )是在保存之前更新order.OrderItems

oi = new OrderItem { Order = order, Item = item, Count = 1 };
order.OrderItems.Add(oi);
context.Orders.Add(order);
context.SaveChanges();

You must add an entry to the database to be invoked on appropriate data. 您必须将一个条目添加到数据库以对适当的数据进行调用。 In this case, it's the OI. 在这种情况下,就是OI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM