0

I have this problem where I have 2 entities connected by foreign key.

  • AEntity: id, idOfEntityB (foreign key, constraint), fields...
  • BEntity: id, fields...

I save both of them to the database with SaveChanges(), later when I try to get AEntity's idOfEntityB, I succeed but when I try to get BEntity according to the id I got from AEntity, I get nothing:

context.AEntities.Add(new AEntity {
   BEntity = new BEntity { ... }
});
context.SaveChanges();
. 
. 
. 
var id1 = context.AEntities.Select(x => x.idOfEntityB);
var bEntities = context.BEntities.Where(x => id1.Contains(x.id));

bEntities has nothing in it. but the fact I was able to have values in id1 is even more confusing since they have foreign key relations (with constraint) and furthermore, id could not be created if it was not saved to the DB.

Later, when I look in the DB I see both entities as should be.

It happens sometimes and I cant reproduce the problem, I cant give more then this as an example since there's a lot of code, I believe it has something to do with caching, and therefore would like to ask if something like that is possible or not and how.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
small
  • 223
  • 1
  • 2
  • 6
  • You better show both classes, since it seems that you are doing wrong everything. – Serge Jan 18 '21 at 21:08
  • I never had such problem, so I dont think I did everything wrong. I would like to get more theoretical explanation of how it may happen – small Jan 18 '21 at 21:11
  • Just hard to explain what is wrong if there is no code. You will not get any help. – Serge Jan 18 '21 at 21:47
  • My question can be summed up to this: is there a way entities are saved to the DB while the context (a different one used from the context that saved) does not hold all of them in completion? – small Jan 18 '21 at 21:53
  • You are the first who says this. You have to show the sample of code that can prove your suggestion, people will say why are you wrong. This is a rule number one here. Nobody is advised to help anybody, who doesn't show what he has done and what is wrong. – Serge Jan 18 '21 at 22:13

1 Answers1

0

is there a way entities are saved to the DB while the context (a different one used from the context that saved) does not hold all of them in completion?

This is likely the issue you are encountering if you are relying on seeing changes between state changes between different DbContext instances. When a DbContext has loaded entities, then another DbContext instance makes changes to those records or the records change behind the scenes in the database, that original DbContext will not refresh the entities from the database.

EF does support the ability to reload entities from the database, but when dealing with child collections it gets a bit more complicated to perform a full refresh. You effectively need to tell the DbContext to forget all of the child collections, stop tracking the parent, clear the parent's child collection, then re-attach and reload the child collection. I recently covered this in the answer for this question: Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db. How to reload the collection?

Ultimately a DbContext lifespan should be kept as short as possible.

Steve Py
  • 26,149
  • 3
  • 25
  • 43