I have some troubles with understanding how Entity Framework is joining entity relations for in-memory entities.
To query with the Entity Framework I disable proxy creation and lazy loading:
public static CoreContext GetReadCoreContext()
{
var context = GetCoreContext();
context.Configuration.ProxyCreationEnabled = false;
context.Configuration.LazyLoadingEnabled = false;
return context;
}
Since I have some inherited types in the ER model and I want to include some entities only when the inherited type is a specific one, I need to do two separate queries. Only a specific type contains a relationship to Localization. Please not that this is only to illustrate why I have to do two separate queries and is not necessarily related to my struggles to understand Entity Framework's mechanism.
Firstly I query the general part, which is not dependent on the specific type:
var myGuid = Guid.Parse("6a81de0b-ce4b-44dc-a693-ca4e13e7d2ab");
using (var ctx = ContextFactory.GetReadCoreContext(TenantId))
{
var entitiesQuery = ctx.MyEntity
.Include(i => i.EntityA)
.Include(i => i.GeneralType);
var myEntity = entitiesQuery.FirstOrDefault(e => e.Id == myGuid);
// check if myEntity.GeneralType is of the specialized type then query depending on the properties of this type
var specificEntity = myEntity.GeneralType as SpecificType;
if (specificEntity != null)
{
var myLocalization = ctx.Localizations.Where(l => l.Id == specificEntity.LocalizationId)
// Entity Framework is automatically setting myEntity.Localization to myLocalization
}
}
What I don't understand is, how does Entity Framework append/set the relation for the in-memory object. Note that the in-memory object is no proxy - otherwise I'd understand why that is working.
When I use .AsNoTracking()
the above described behavior is not working.