【发布时间】:2012-02-29 00:42:35
【问题描述】:
首先,我的 UoW 继承自 DbContext。
我转而使用 Unity.Mvc3 并将这一行添加到我的注册中:
Container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
一旦我搬到这里,我对实体框架的延迟加载似乎停止了工作。这是一个例子:
public class Process
{
public Guid Id {get;set;}
public virtual Guid? StatusId {get;set;} //this is the FK EF uses.
public virtual Status Status {get;set;}
}
public class Status
{
public Guid ID {get;set;}
public Name {get;set;}
}
//DTOs
public class ProcessDto
{
public Guid Id {get;set;}
public Guid? StatusId {get;set;} //this is the FK EF uses.
public StatusDto Status {get;set;}
}
public class StatusDto
{
public Guid ID {get;set;}
public Name {get;set;}
}
在我们调用 Cre 之前,Satuse 被加载到上下文中
//Process Service
public ProcessDto Create(ProcessDto dto)
{
var processEntity = new Process(){StatusId = dto.StatusId}
processRepository.Add(processEntity)
unitOfWork.Commit()
//at this point, before using Unity.Mvc3 the Status would be proxied into the processEntity. Moving to Unity.Mvc3 it is like Lazy Loading stopped working.
return Mapper.Map<Process,ProcessDto>(processEntity);
}
单步执行代码直到调用 Create 之后请求才会终止。不知道有没有人遇到过类似的情况。
【问题讨论】:
标签: asp.net-mvc-3 entity-framework dependency-injection entity-framework-4.1 unity-container