0

I am adding new entity to the context and I would like populate all its references collections once the add is done. Issue is, I am reading the same entity from the context which I created during the add(), basically EF doesn't go to the DB. This is correct behaviour, but how do I get around it ?

    Repo().Add(newEntity);
    Repo().Reload(newEntity);  

This reloads the entity from DB however I am not getting the references (FK relations). I have found how to load the reference, however I would need a generic way how to load all the references for any entity.

var entry = Context.Entry(entity);
entry.Reference("ReferenceName").Load();

Is the above the correct approach or is there some other way ?

4

1 回答 1

1

没有看到您的存储库代码,我猜这是一个延迟加载问题。

该站点解释了急切与延迟加载:http: //msdn.microsoft.com/en-us/data/jj574232.aspx

您可以通过在存储库中使用 .Include() 来准确指定要返回的引用(适用于不需要所有内容的长链)。

context.Set<whateverType>.Include(t => t.(whatever you are referencing)).Where(t => t.id = id);

或者,您可以指定使用预加载的上下文,并通过检索带回所有内容。

context.LazyLoadingEnabled = false;
于 2013-08-22T16:22:45.757 回答