Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using AsExpandable() cause AsNoTracking doesn't work. #12

Closed
msanaei opened this issue Jan 17, 2015 · 3 comments
Closed

using AsExpandable() cause AsNoTracking doesn't work. #12

msanaei opened this issue Jan 17, 2015 · 3 comments

Comments

@msanaei
Copy link

msanaei commented Jan 17, 2015

I'm using EF code first, I worked with detached entities and for updating my entities I used GraphDiff (that works with detached entities), so when I want to load my entities I use AsNoTraking():

 _uow.Repository<WorkCenterCapacity>()
                .FindAllQueryable()
                .Where(/*some criteria*/) 
                .GroupBy(x => x.WorkCenter.Code)
                .AsNoTracking()
                .ToDictionary(x => x.Key, x => x.FirstOrDefault());
//do some changes
// update using graphDiff

It worked correctly and I was be able to update my detached entity using graphDiff.

But, recently I used LINQKit to use Predicate in my query so I changed above query to:

 _uow.Repository<WorkCenterCapacity>()
                .FindAllQueryable()
                .Where(workCenterListCriteria) //predicate
                .AsExpandable()  //<----------- this line cause AsNoTracking doesn't work
                .GroupBy(x => x.WorkCenter.Code)
                .AsNoTracking()
                .ToDictionary(x => x.Key, x => x.FirstOrDefault());
//do some changes
// update using graphDiff

but now when graphDiff wants to update the loaded WorkCenterCapacity entity I get following Error:

A first chance exception of type 'System.InvalidOperationException' occurred in RefactorThis.GraphDiff.dll

Additional information: GraphDiff supports detached entities only at this time. Please try AsNoTracking() or detach your entites before calling the UpdateGraph method

So it seems that AsExpandable cause AsNotracking doesn't work.

@axelheer
Copy link
Contributor

Try to call AsNoTracking first, then AsExpandable. If i remember correctly, AsNoTracking casts the current query to some Entity Framework internal class, which cannot work since it's wrapped by AsExpandable...

@Thorium
Copy link
Collaborator

Thorium commented Jan 20, 2015

Or you can call Configuration.AutoDetectChangesEnabled = false; in your context constructor, so that tracking is always off.

@msanaei
Copy link
Author

msanaei commented Jan 21, 2015

@axelheer 👍 Thank you, I checked it, the problem solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants