Retrieve object from LINQ-to-SQL DataContext before SubmitChanges

后端 未结 1 1013
挽巷
挽巷 2021-01-14 07:34

I insert a new object into LINQ-to-SQL DataContext without calling SubmitChanges() yet:

MyDataContext db = new MyDataContext();
MyObject newObject = new MyOb         


        
相关标签:
1条回答
  • 2021-01-14 08:21

    Try:

    db.MyObjects.SingleOrDefault(o => o.Id == 1);
    

    You'd think this would be the same, but see these two connect issues:

    • LINQ-to-SQL doesn't use cache for lookup
    • LINQ-to-SQL still roundtrips for ID lookups (3.5SP1)

    If that doesn't work, I know that the .Single(o => o.Id == 1) works in 3.5 SP1 (I don't know if I tried .SingleOrDefault(pred)). Apparently the .Where(o => o.Id == 1).Single() is fixed in 4.0 - again, I haven't tried .Where(pred).SingleOrDefault().

    0 讨论(0)
提交回复
热议问题