3

I recently upgraded our solution from EF4.1 to EF6. Our previous create method added the detached object to the context, saved changes, then requeried the object based on the new id. We requery the object because we do not use lazy loading, and use includes/selects to get navigational properties - which are added by taking the IDbSet and returning an IQueryable. In short, I cannot just return the existing in-memory copy - because it would not be fully loaded up. I have tried detaching the in-memory copy. I cannot flush the dbcontext (because I am working with other objects in the same context).

Anyway, the big issue, my big issue is that in the EF6 branch when I requery that object it returns a null record - not finding the one I just created. The record is persisted to the database - I can see it. If I create a new dbcontext and query for that record I can find it.

dbContext.Set<TEntity>().Add(model);

dbContext.SaveChanges();

var newCopy = dbContext.Set<TEntity>().SingleOrDefault(p => p.Id == model.Id);

In this instance, newCopy is always null. But if I get a new dbContext - I can get the object fine.

Anyway, I need to solve this issue. Any ideas?

11
  • Might not be related, but I got problems with queries when I upgraded my EF 4.1 project to EF 6.1. Solution was targeting the 4.5.1-Framework in the project. Did not work with only 4.5. Commented Jan 16, 2015 at 14:55
  • Hi Stephan - everything else seems to be working fine. We are using .NET 4.0. It is just really this requery issue - and a few issues with deeply nested includes. Commented Jan 16, 2015 at 14:57
  • 1
    Please could you post the code? Commented Jan 16, 2015 at 14:59
  • Hi buffjape - I did just above. I just omitted the dbcontext and getting of the set. dbContext.Set<TEntity>().Add(Model); dbContext.SaveChanges(); dbContext.Set<TEntity>().SingleOrDefault(p => p.Id = Model.Id); side note: I have tried a where also - nothing queries the object short of a new dbContext. Commented Jan 16, 2015 at 15:04
  • 2
    does _context.Set<TEntity>().Find(id) return anything? (where id is an object) Commented Jan 16, 2015 at 15:19

2 Answers 2

1

I actually discovered what the issue was/is. In EF4, it really doesn't matter if the mappings are HasRequired or HasOptional. However, if you use a HasRequired on a nullable integer field (legacy db) - then it actually is performing an inner join and will not select the record - resulting in a return of null. Since I am not using lazy loading - but rather eager loading - if I have an include on the above described mapped field - it returns a null.

Thanks everyone for their help - it was appreciated!

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm... the code in the OP did not have any .Include extensions... so did the code in the OP actually return null or not?
Hey dan - to simplify the code I put the Set<> in directly... I had forgoten that my GetSet method (which returns the Set) actuall makes another call out to includes. It was the multiplicity of the mappings that got me. Thanks for your help though - it all pointed me toward a solution... When I did try it as stated above, I got a result.
0

Does the following work:

dbContext.Set<TEntity>().Add(model);

dbContext.SaveChanges();

var newCopy = dbContext.Set<TEntity>().Find(model.Id);

I won't claim to know the ins an outs of it but I am using that in the same situation and it works.

You could also check that the model properties are not virtual as EF6 takes this as lazy loading (You may have already done this).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.