Linq2Sql多对多关系与问题插入新对象 [英] Problem with Linq2Sql Many-to-Many relationship & Inserting new objects

查看:76
本文介绍了Linq2Sql多对多关系与问题插入新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个简单的linq 2 sql多对多,插入一些数据,进行操作.

i'm trying to do a simple linq 2 sql many-to-many, insert some data, operation.

这是代表许多对多股票的Northwind模型:

here's the stock Northwind model representing a many to many:

替代文字http://www.iaingalloway.com/images/linq-detail .jpg

现在我要执行的操作是插入一个新订单,如果该产品不存在,则在同一笔交易中同时插入该订单.我得到的错误是:

Now what i'm trying to do is insert a new order and if the product doesn't exist, then insert that at the same time, within the same transaction. The error i'm getting is:

System.Data.Linq.DuplicateKeyException: Cannot add an entity with a
 key that is already in use.

这是我的(pseduo)代码:

So this is my (pseduo) code:

using (SqlContext db = new SqlContext())
{
    // Get existing or create a new instance.
    Order newOrder = GetOrder(order.Id) ?? new Order();
    // Left to right stuff.
    newOrder.Foo = order.Foo;

    // Now associate this new order to a product (which might not exist).
    if (!order.ProductList.IsNullOrEmpty())
    {
        // We have some products...

        IList<Order_Detail> orderDetailList = new List<Order_Detail>();
        foreach(Models.Product product in order.ProductList)
        {
            // Associate each product to the a new order_detail.
            orderDetailList.Add(new Order_Detail
                                    {
                                        Product = new SqlContext.Product
                                                      {
                                                          Foo = product.Foo
                                                      }
                                    });
        }

        // Now associate all the order_details to this order.
        newOrder.Order_Details.AddRange(orderDetailList);

        if (newOrder.Id <= 0)
            db.InsertOnSubmit(newOrder);

        db.SubmitChanges();   // <-- exception throw here.
    }
}

我假设我需要先保存产品,然后再尝试保存订单?我很困惑:(

I'm assuming i need to save the products first before i try and save the order? I'm so confused :(

推荐答案

// Associate each product to the a new order_detail.
orderDetailList.Add(new Order_Detail
{
    Product = new SqlContext.Product
    {
        Foo = product.Foo
    }
});

这里错误的一件事是,您创建了一个要在Order_Detail.Product属性上设置的新产品.相反,您应该从数据库中获取即将来临的产品并将其设置在属性上.

One thing that is wrong here, is that you create a new product to set on your Order_Detail.Product property. Instead , you should take the product that's comming from the database and set it on the property.

我不确定里面有什么order.ProductList-如果这些产品是从数据库中加载的,则应将它们直接设置为Order_Detail.Product,而不要执行新的SqlContext.Product.

I'm not sure what order.ProductList has inside - if these products are loaded from the database then you should set them directly to your Order_Detail.Product instead of doing new SqlContext.Product.

@jfar L2S确实支持多对多关系,您只是在Order上没有属性Products(在这种情况下,这实际上是一件好事,因为OrderDetails具有数量和其他属性).

@jfar L2S does support many-to-many relationships , you just can't have a property Products on your Order ( in this case this is actually a good thing because OrderDetails has Quantity and other properties).

这篇关于Linq2Sql多对多关系与问题插入新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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