There is an interesting feature exists in entity framework core:
Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded.
This is nice in some cases. However at the current moment I'm trying to modelling many-to-many relation with advanced syntaxic additions and wan't to check, that the mapping I create work well.
But I actually can't do that, since if let's say 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 due to auto-fixup loaded.Rel
field already will be populated, even if I don't include anything. So with this feature I can't actually check nothing. Can't check that I use proper mapping, and my additions to Include
works properly. Having thouse in mind, what should I change to be able to actaully test my navigation properties work properly?
I create a testcase which should be passing, but now failing. Exact code could be found there
I'm using .Net Core 2.0 preview 1 and EF core according to that.
DbContext
which has no entities created and do the second query on it, though you need to inject it'sDbContextOptions
and pass it to its constructor – RanknewCtx.Models1
is empty for new context in inmemory case. at least for me, maybe I'm doing something wrong. – Bohman