C# 4.0 实体框架Context.Configuration.AutoDetectChangesEnabled更新问题?

C# 4.0 实体框架Context.Configuration.AutoDetectChangesEnabled更新问题?,c#-4.0,entity-framework-5,unit-of-work,C# 4.0,Entity Framework 5,Unit Of Work,我必须将大约30个缺少的数据从电子表格导入MSSQL数据库。我使用实体框架将记录插入/更新到数据库中。但是默认的实体框架配置的性能非常慢。约束条件是,我需要在插入表之前验证记录。如果存在,则应使用新值更新,否则应将新记录插入数据库。但在数据库中插入/更新记录需要花费大量时间。我找到了加速这一过程的解决方案 上述设置在速度上产生了巨大的差异 但最大的问题是,当我将AutoDetectChangesEnabled设置为false时,表中的记录并没有更新,但插入功能完全正常 还有人看到这个问题吗?有人

我必须将大约30个缺少的数据从电子表格导入MSSQL数据库。我使用实体框架将记录插入/更新到数据库中。但是默认的实体框架配置的性能非常慢。约束条件是,我需要在插入表之前验证记录。如果存在,则应使用新值更新,否则应将新记录插入数据库。但在数据库中插入/更新记录需要花费大量时间。我找到了加速这一过程的解决方案

上述设置在速度上产生了巨大的差异

但最大的问题是,当我将AutoDetectChangesEnabled设置为false时,表中的记录并没有更新,但插入功能完全正常


还有人看到这个问题吗?有人帮我解决这个问题吗?

我已经用下面的代码解决了这个问题。条目。当AutoDetectChangesEnabled设置为false时,状态变为未更改

public virtual void Update(T entity)
    {
        //DbSet.Attach(entity);
        //context.Entry(entity).State =EntityState.Modified;


        if (entity == null)
        {
            throw new ArgumentException("Cannot add a null entity.");
        }

        var entry = context.Entry<T>(entity);

        if (entry.State == EntityState.Detached)
        {
            var pkey = DbSet.Create().GetType().GetProperty(entity.GetType().Name + "ID").GetValue(entity);

            var set = context.Set<T>();
            T attachedEntity = set.Find(pkey);  // You need to have access to key

            if (attachedEntity != null)
            {
                var attachedEntry = context.Entry(attachedEntity);
                attachedEntry.CurrentValues.SetValues(entity);
            }
            else
            {
                entry.State = EntityState.Modified; // This should attach entity
            }
        }
        else if (entry.State == EntityState.Unchanged)
        {
            entry.State = EntityState.Modified;
        }

    }`
public virtual void Update(T entity)
    {
        //DbSet.Attach(entity);
        //context.Entry(entity).State =EntityState.Modified;


        if (entity == null)
        {
            throw new ArgumentException("Cannot add a null entity.");
        }

        var entry = context.Entry<T>(entity);

        if (entry.State == EntityState.Detached)
        {
            var pkey = DbSet.Create().GetType().GetProperty(entity.GetType().Name + "ID").GetValue(entity);

            var set = context.Set<T>();
            T attachedEntity = set.Find(pkey);  // You need to have access to key

            if (attachedEntity != null)
            {
                var attachedEntry = context.Entry(attachedEntity);
                attachedEntry.CurrentValues.SetValues(entity);
            }
            else
            {
                entry.State = EntityState.Modified; // This should attach entity
            }
        }
        else if (entry.State == EntityState.Unchanged)
        {
            entry.State = EntityState.Modified;
        }

    }`