开发者

LINQ DataContext inserts object that is not attached

开发者 https://www.devze.com 2023-01-04 21:41 出处:网络
I have created two separate objects, one of class Order and one of class TempOrder. When I tryto insert the TempOrder object in the db using db.Temp开发者_JAVA百科Orders.InsertOnSubmit(obj) and then c

I have created two separate objects, one of class Order and one of class TempOrder. When I try to insert the TempOrder object in the db using db.Temp开发者_JAVA百科Orders.InsertOnSubmit(obj) and then calling db.SubmitChanges the Order object gets submitted as well (without being attached to the datacontext or anything). There is no relationship between the two objects in the database, but I've created a simple member function in the classes to create Order object when you have the TempOrder and vice versa (toNewOrder - toNewTempOrder). The code I'm using can be seen below:

if(order.PaymentType == "Paypal")
{
    TempOrder temp = new TempOrder();
    temp = order.ToNewTempOrder();
    db.Add(temp);//Add is calling datacontext.TempOrders.InsertOnSubmit and datacontext.SubmitChanges
}

When db.Add(temp) is called the changeset contains the temp object and the order object as well. How can I avoid this? Am I doing anything wrong? Is this because of the function .toNewTempOrder()? I'm stuck on this.


How did your code obtain that order object we see in the snippet?

Are you sure that one is not deriving from the other in your C# code? SQL tables they represent can still be totally unconnected if C# classes were hand crafted.

Is that db.Add(...) overloaded or it takes some base type and then makes a decisions based on the actual type of the argument?

0

精彩评论

暂无评论...
验证码 换一张
取 消