简体   繁体   中英

C# Entity Framework LINQ Check if Context is Detached before RemoveRange with List of Objects

I am writing a LINQ to SQL to do a RemoveRange . The following is working.

ctx.Table1Set.AttachRange(records);
ctx.Table1Set.RemoveRange(records);

I have to do AttachRange or my RemoveRange will throw an exception that it does not exists in the ObjectManager .

I would like to check if my ctx is attached or not attached with the parameter, LIST of records which is a list of two fields, ID and CODE

1  1001
2  1002 
3  1003

I researched on other post using the following code

var entry = context.Entry(ug);
if (entry.State == EntityState.Detached)
{
    context.MY_GROUPS.Attach(ug);
}

but this is for one object and not a list of objects.

Is there a way to check the context state with a list?

If I have a List<Table1> aka list of records,

How can I check if the context, ctx, not attached with a LIST of objects?

由于信誉要求,我无法评论,但是调试此错误的一种方法是在newrecords上尝试foreach,然后在try catch中的记录上尝试.remove()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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