Closed epozsh closed 10 months ago
Hello @epozsh ,
After reviewing the Fiddle, there is currently no issue within the library.
The problem is caused because the following part customersIds.Select(cId => new Customer { CustomerID = cId });
is an Enumerable.
This means customer get materialized twice:
context.Customers.AttachRange(entitiesToBeDeleted);
context.Customers.RemoveRange(entitiesToBeDeleted);
This is equivalent of doing the foreach
twice which doesn't work either:
foreach(var entityToBeDeleted in entitiesToBeDeleted) {
context.Customers.Attach(entityToBeDeleted);
}
foreach(var entityToBeDeleted in entitiesToBeDeleted) {
context.Customers.Remove(entityToBeDeleted);
}
Fiddle: https://dotnetfiddle.net/Wu7USz
To solve it, you can simply to a ToList
first:
var entitiesToBeDeleted = customersIds.Select(cId => new Customer { CustomerID = cId });
context.Customers.AttachRange(entitiesToBeDeleted);
context.Customers.RemoveRange(entitiesToBeDeleted);
Let me know if that explains correctly the cause and solution for this issue.
Best Regards,
Jon
Hello getting this error
while trying use
context.Customers.AttachRange(entitiesToBeDeleted); context.Customers.RemoveRange(entitiesToBeDeleted);
but when using this
foreach(var entityToBeDeleted in entitiesToBeDeleted) { context.Customers.Attach(entityToBeDeleted); context.Customers.Remove(entityToBeDeleted); }
it works as expected
You can check it here
https://dotnetfiddle.net/9j6AqI
Any help?