实体框架上下文和结构图处理

时间:2011-09-07 22:14:56

标签: entity-framework asp.net-mvc-3 entity-framework-4.1 structuremap

我在asp.net mvc应用程序中处理实体框架连接时遇到了奇怪的问题。

我的结构很简单,例如:

实体:

public class EmployeeReport
{
    public int EmployeeReportId { get; set; }

    public DateTime Created { get; set; }

    public Decimal Hours { get; set; }

    public string Comment { get; set; }

    public int EmployeeId { get; set; }

    public int ContractId { get; set; }

    public int ServiceId { get; set; }

    public virtual ReportContract Contract { get; set; }

    public virtual ReportService Service { get; set; }

    public virtual Employee Employee { get; set; }

}

实体映射器:

public class EmployeeReportMapper : EntityTypeConfiguration<EmployeeReport>
{
    public EmployeeReportMapper()
    {
        ToTable("intranet_employee_reports");
        HasKey(x => x.EmployeeReportId);

        Property(x => x.Created).HasColumnName("Created").IsRequired();
        Property(x => x.Comment).HasColumnName("Comment").IsOptional();
        Property(x => x.Hours).HasColumnName("Hours").IsRequired();

        HasRequired(x => x.Employee).WithMany().HasForeignKey(x => x.EmployeeId);
        HasRequired(x => x.Service).WithMany().HasForeignKey(x => x.ServiceId);
        HasRequired(x => x.Contract).WithMany().HasForeignKey(x => x.ServiceId);
    }
}

DbContext - interface

public interface IDbContext : IDisposable
{
    IDbSet<EmployeeReport> EmployeeReports { get; }
}

DbContext - 实现

public class IntranetDbContext : DbContext,IDbContext
{
    public IDbSet<EmployeeReport> EmployeeReports { get; set; }
    ...


    public IntranetDbContext() : base("IntranetDb")
    {
        Database.SetInitializer<IntranetDbContext>(null); 
    }

    public void Commit()
    {
        SaveChanges();
    }

    public void ChangeEntityState(object entity, EntityState entityState)
    {
        Entry(entity).State = entityState;
    }

    public void ExecuteSql(string query, SqlParameterCollection parameterCollection)
    {
        Database.ExecuteSqlCommand(query, parameterCollection);
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        /* Register custom mapping class */
        modelBuilder.Configurations.Add(new EmployeeReportMapper());
         ....
        base.OnModelCreating(modelBuilder);
    }

}

最后我的结构图配置:

public class CoreRegistry : Registry
{
    public CoreRegistry()
    {
        For<IDbContext>().HttpContextScoped().Use<IntranetDbContext>();
        ...
    }
}

和Global.asax:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
    }

好的,现在问题,在我的应用程序中我使用标准构造函数依赖注入 或者调用ObjectFactory.GetInstance()。

在我的一个控制器中,我调用了服务类,它可以访问dbcontext并获取一些entites。

不幸的是我得到了经典例外:

  

ObjectContext实例已被释放,无法再使用   对于需要连接的操作。

这很奇怪,因为在请求期间调用了服务,所有数据都被强制转换为控制器中的客户端......

任何想法,我错误的地方?

编辑:

服务代码:

public class EmployeeService : IEmployeeService
{
    /// <summary>
    /// IDbContext reference
    /// </summary>
    private readonly IDbContext _dbContext;

    public EmployeeService(IDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public List<Employee> GetSubordinateEmployees(Employee employee)
    {
        List<Employee> employees = new List<Employee>();

        foreach (var unit in employee.OrganizationUnits.ToList()) /* throw exception*/
        {
            foreach (var childrenUnit in unit.ChildrenUnits)
            {
                employees.AddRange(childrenUnit.Employees);
            }
        }
        return employees.Distinct().ToList();
    }

控制器:

    private readonly IEmployeeService _employeeService;

    public EmployeeReportController(IEmployeeService employeeService)
    {
        _employeeService = employeeService;
    }

    [HttpGet]
    public ActionResult SearchReports()
    {
        List<Employee> employees = _employeeService.GetSubordinateEmployee(IntranetSession.Current.LoggedAccount.Employee).ToList(); // Exception!
       ...

        return View();
    }


}   

1 个答案:

答案 0 :(得分:1)

您的代码根本不使用当前的DbContext。问题是:

IntranetSession.Current.LoggedAccount.Employee

其次是:

employee.OrganizationUnits.ToList()

存储在会话中的员工已加载了已经处理但仍保留对该上下文的引用的上下文。当您加载该员工时,您并没有急切地加载他的组织,因此一旦您访问她OrganizationUnits,它将尝试在已处置的上下文中触发延迟加载。

有两种方法可以避免此问题:

  • 急切加载您在会话中存储的员工需要使用的所有信息。这意味着检索员工context.Employees.Include(e => e.OrganizationUnits).Single(...)
  • 仅在会话中存储员工的ID并加载您需要的按需数据

如果要在会话中缓存整个员工,请确保通过调用以下方法禁用存储在会话中的对象的代理创建:

 context.Configuration.ProxyCreationEnabled = false;

它将确保缓存的数据不会继续引用已处置的上下文(btw。阻止垃圾收集器收集上下文及其所有引用的对象)。

相关问题