English 中文(简体)
使用 EF 代码第一到会话, 使用 Basin 类, 使用 EF 代码第一会话, 加载对象等级级
原标题:Load Object Hierarchy with Base Class using EF Code First into Session

我在问题 。 [https://code>List <OfpoterClass>来装载衍生的类别属性。 方法(这一直 < a href="http://data.uservoice.com/forms/72025-entity-entframramwork-fer-fetature-sult-suppet-suggs/suggs/suggs/s/s/1249-suggs/12492-suppings/124/12/1289-supps/1289-incts/

我绕着这里工作 通过循环我的收藏 我的收藏 包含混合的 兄弟姐妹类型, 单个调用

ctx.Entry(myDerivedType).Reference("PropOfDerivedType").Load();

这导致许多额外的往返访问数据库,但至少是起作用了。

现在我需要装入这样的对象等级, 并将其保存在 MVC 网络应用程序的会话中。 由于 < code> DbContext 不应该是会话的一部分, 因此我 < em> 相信 , 我必须装入对象图 < code> AsNoTracking () , 比如 :

var myGraph = (from my in ctx.MyHierarchyRoot.Include("NonDerivedStuff")
               .AsNoTracking()
               where CONDITION select my).Single();

<强度问题1

如果对象图形要存储在会话中, 是否有必要使用 < code>. AsNoTrading () ?

然后我再通过从中得来的兄弟姊妹 在等级制度里转身, 并试图装载他们像这样:

foreach (SomeBase b in myGraph.SomeCollection)
{
    if (b is SomeConcreteDerivedType)
    {
        ctx.Entry(b).Reference("SomePropertyOfSomeConcreteDerivedType").Load();
    }
}

然而,由于我用.AsNoTracking ()装载我的格子,我明白了:

Member Load cannot be called for property SomePropertyOfSomeConcreteDerivedType because the entity of type SomeConcreteDerivedType does not exist in the context. To add an entity to the context call the Add or Attach method of DbSet.

<强问题2

假设问题1的答案是“是”,我如何正确装载衍生物类型的导航特性?

最佳回答

如果对象图形要存储在会话中, 是否有必要使用 < code>. AsNoTrading () ?

有必要确保会场中存储的实体不提及上下文(也不需要上下文 ) 。 这应该由拆卸实体在将实体保存到会场之前进行,但拆卸总是会断绝关系。 在这种情况下,最简单的办法可能是取消代理机构的创建(懒惰的加载和动态的变更跟踪不会起作用 ) 。

另一种办法是以正常方式装入实体图,并创建图中的深克隆(序列化),深克隆总是与上下文完全分离,并且可以安全存储在会话中。

问题回答

暂无回答




相关问题
named connection not found (Entity framework problem)

I m building multi-project Application where some UserControl, the user control has a Entitymodel object (myDBContainer db = new myDBContainer()), when i drop my user control on my a form i got the ...

EF4 POCO - Updating a navigation property

I have a Recommendation object and a FeedbackLevel object and the FeedbackLevel object is a navigation property inside a Recommendation object Recommendation int EntityKey; FeedbackLevel Level; ...

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven t found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the ...

Using sql date data type and EF4

I have a table w/ a sql date data type. When I look at the EDM markup, the storage element reflects this. The conceptual entity has a data type of DateTime (there doesn t appear to be a Date data type)...

RIA services, EF and stored procs

I have this problem for 2 months now How can I use stored procedures whith RIA. I was using LinqToSql and everithing works fine. I ve made an class in designer and mapped it to a SP. Now in EF I ...

Mocking Entity Context in EF4

I am using VS2010 B2 and EF4 B2 and trying to use Rhino Mocks to mock the entity context generated by EEF. var context = MockRepository.GenerateMock<SomeDBEntities>(); IObjectSet<TxMode> ...

热门标签