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.