An interesting function exists in the core of entity structures:
Entity Framework Core will automatically set navigation properties to any other objects that were previously loaded into the example context. Therefore, even if you do not explicitly include data for the navigation property, the property can still be populated if some or all of the related objects have been loaded before.
This is good in some cases. However, at the moment I am trying to model a many-to-many relationship with advanced syntactic additions and not verify that the generated mapping works well.
But I really canโt do this, because if I say that I have something like:
class Model1{ ... // define Id and all other stuff public ICollection<Model2> Rel {get; set;} } Model1 m1 = new Model1(){Id=777}; m1.Rel.Add(new Model2()); ctx.Add(m1); ctx.SaveChanges() var loaded = ctx.Model1s.Single(m => m.Id == 777);
so because of the auto-fixup loaded.Rel loaded.Rel will already be filled even if I donโt add anything. Therefore, with this function, I can not verify anything. I canโt verify that I am using the correct mapping, and my additions to Include work correctly. Bearing in mind that I have to change in order to be able to effectively validate my navigation properties correctly?
I am creating a test file that should pass, but now does not work. The exact code can be found there.
I am using .Net Core 2.0 1 preview and EF core.
source share