Let’s say I have an entity Foo.
public class Foo
{
public int Id {get;set;}
public virtual ICollection<Bar> Bars {get;set;}
}
When I get this entity from the db then I actually get a proxy and everything works as it should be.
But, can I attach Foo to DbContext and create the proxy manually so Bars would not return null after attaching it, but an actual collection of bars?
Meaning
var foo = new Foo { Id = 1 };
Context.Set<Foo>().Attach(foo);
foo = CreateProxyOf(foo);
var countOfBars = foo.Bars.Count();
Is there something like CreateProxyOf?
Yes there is a way but instead of Foo’s constructor you must use: