【问题标题】:EntityFramework fail on update entity - cannot be trackedEntityFramework 更新实体失败 - 无法跟踪
【发布时间】:2021-05-28 15:38:48
【问题描述】:

我的应用程序遇到了这个已知问题。 我已经对 HasKey 和 HasAlternateKey 进行了很多更改,甚至是 HasIndex、Unique 和 OnModelCreateCreating() 方法的其他变体。 我什至重新映射了我的实体,但仍然没有成功!

无法跟踪实体类型“Imoveis”的实例,因为 另一个具有键值“{Cod: 91}”的实例已经存在 跟踪。附加现有实体时,确保只有一个实体 附加了具有给定键值的实例。

我的 BaseRepository 类的 Update() 方法:

    public T Update(T entity)
    {
        DbContext.Entry(entity).State = EntityState.Modified; <-- Exception happens here
        DbContext.SaveChanges();
        return entity;
    }

如何调用 Update()。这个问题发生在两个电话上。第二次尝试是防止此错误的失败方法:

public Imoveis InsertOrUpdateUniqueName(Imoveis imoveis)
{
    try
    {
        if (imoveis.Cod > 0)
            Update(imoveis);
        else
            Insert(imoveis);
        return imoveis;
    }
    catch (Exception ex)
    {
        if (imoveis.Cod > 0)
        {
            if (ex is DbUpdateException)
            {
                if ((ex.InnerException as SqlException)?.Number == 2627)
                    return UpdateImovelDuplicado(imoveis);
            }
            else if (ex is InvalidOperationException)
            {
                var local = DbContext
                    .Set<Imoveis>()
                    .Local
                    .FirstOrDefault(x => x.Cod.Equals(imoveis.Cod));
                if (local != null)
                    DbContext.Entry(local).State = EntityState.Detached;
                return Update(imoveis);
            }
        }

        throw ex;
    }
}

【问题讨论】:

  • 我想说 SQL 在这里是无关紧要的。您能否说明Update 是如何被调用的以及entity 来自哪里?
  • @GuruStron 谢谢!我已经更新了 Update() 调用。我正在维护 SQL,因为其他人可以使用它。
  • 您如何管理 DbContext 生命周期?
  • @DavidBrowne-Microsoft 我正在使用在依赖注入设置期间创建的单个 DbContext 实例并通过 ServiceCollection.AddDbContext() 注入它
  • 你不能使用单例。采取look here

标签: c# sql sql-server entity-framework orm


【解决方案1】:

解决了!

解决方案:

1 - 在我的案例(控制台应用程序)中,我删除了异步插入或更新,因为我使用的是单例 DbContext。但这不是强制性的,取决于具体情况。您需要了解 DbContext 的生命周期。

2 - 我更改了我的 Update() 方法以从 DbContext Set 集合中恢复和分离原始对象。

这是我的新 Update() 方法,它为通用对象调用了新的 DetachLocal() 方法。

public T Update(T entity)
{
    DetachLocal(entity);
    DbContext.SaveChanges();
    return entity;
}

public void DetachLocal(T t)
{
    var keyname = GetIdName(t);
    var keyValue = GetIdValue(t);

    var local = DbContext.Set<T>()
        .Local
        .FirstOrDefault(entry => GetIdValue(entry).Equals(keyValue));
    DbContext.Entry(local).State = EntityState.Detached;
    DbContext.Entry(t).State = EntityState.Modified;
}

感谢@Crowcoder 和@DavidBrowne-Microsoft 的回复让我找到了这个解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-09
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多