Last post May 13, 2015 05:08 AM by steschu
May 10, 2015 09:44 AM|steschu|LINK
I have 1:n relationship for Category, Subcategory and Products in my Entity Model.
In order so solve this problem:
I loaded each table into a List<T>:
List<Category> c = (from q in context.Categories select q).ToList()
List<Subcategory> s = (from q in context.Subcategories select q).ToList()
List<Product> p = (from q in context.Products select q).ToList()
and then tried to manipulate those (delete and add entities). Unfortunately I recognized that every Add() and Clear() will cause a query to the database whether the foreign key constraint is valid. This is time wasting. How can I detach my data from the
context completely? I'm using EF5 - so there is no Detach method in the object context. Using context.Categories.AsNoTracking() did not work either.
Thanx for help,
May 11, 2015 02:01 AM|Zhi Lv - MSFT|LINK
With reference to the following articles, I found that we can detach objects from the object context by calling the
System.Data.Objects.ObjectContext.Detach(System.Object) method. For more details, please see:
May 13, 2015 05:08 AM|steschu|LINK
Thanx for your help. But the the mentioned Detach-methods do not seem to exist in EF5.
Instead I finally found out this way to solve the issue: after loading all into Lists, you have to change the EntityState of each Entity:
p.ForEach(p => context.Entry(p).State = EntityState.Detached);
s.ForEach(s => context.Entry(s).State = EntityState.Detached);
c.ForEach(c => context.Entry(c).State = EntityState.Detached);