繁体   English   中英

Include()性能之后的实体框架Attach()

[英]Entity Framework Attach() after Include() performance

急于加载实体并在单独的上下文中重新附加它之后,我遇到了非常严重的性能问题。

下面的例子。 首次招募公司时,员工会很忙。 有数千名员工。

然后,需要花费几秒钟的时间将公司附加到第二个环境中。

Company company;

using(var context = new MyEntities())
{
    company = context
        .Companies
        .Include(x => x.Employees)
        .Single(x => x.CompanyId = someCompanyId);
}

// Stuff happens here

var newEmployee = CreateNewEmployee();

using(var context = new MyEntities())
{
    context.Configuration.AutoDetectChangesEnabled = false;
    context.Companies.Attach(company);  // <<-- Extremely slow
    company.Employees.Add(newEmployee);
    context.SaveChanges();
}

我将如何分离员工名单?

编辑:已加载的员工将没有任何更改(新添加的员工除外)

关闭AutoDetectChanges

context.Configuration.AutoDetectChangesEnabled = false;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM