简体   繁体   English

两个对象的 EF Core 保存错误取决于第二个相等的对象

[英]EF Core Saving error for two objects depending on a second equal one

I have a vey long long POCO class ("FichaCliente") that I'll simplify here emphasizing the property that's an ObservableCollection:我有一个很长很长的 POCO 类(“FichaCliente”),我将在这里简化,强调 ObservableCollection 的属性:

{
  public int FichaCliente1 { get; set; }
  ...
  public int FichaClienteN { get; set; }

  public ObservableCollection<Reclamado> Reclamados { get; set; }

  public int FichaClienteN+2 { get; set; }
  ...
  public int FichaClienteN+M { get; set; }

} }

The "Reclamado" class is also a relative long POCO that has another class as element, the UF class. “Reclamado”类也是一个相对较长的 POCO,它有另一个类作为元素,即 UF 类。 Thus we can understand the "Reclamado" as being:因此我们可以将“Reclamado”理解为:

{
  public int Reclamado1 { get; set; }
  ...
  public int ReclamadoN { get; set; }

  public UF Uf { get; set; }

  public int ReclamadoN+2 { get; set; }
  ...
  public int ReclamadoN+M { get; set; }

} }

Being UF class a simple POCO as follows:作为 UF 类,一个简单的 POCO 如下:

{
    public int Id { get; set; }
    public string Apelido { get; set; }
    public string Descricao { get; set; }
    public string Municipio { get; set; }
}

The SAVE operation to the database is handled by just the "SaveFichaAsync" method that's as simple as:对数据库的 SAVE 操作仅由“SaveFichaAsync”方法处理,非常简单:

public async void SaveFichaAsync(FichaCliente ficha)
    {
        using (var context = new DataContext(_dBService))
        {
            context.FichasClientes.Update(ficha);
            await context.SaveChangesAsync();

            return;
        }
    }

What's a sure thing is that "FichaCliente" can have many "Reclamado" as it's needed.可以肯定的是,“FichaCliente”可以根据需要拥有许多“Reclamado”。 And that's fine, it works!这很好,它有效! Just to make it clear, the UF entity is the equivalent to me as what would be states in the US, thus, a given address belongs to a state (city, state...).明确地说,UF 实体与我等同于美国的州,因此,给定地址属于州(城市、州......)。 As "Reclamados" are distinct entities, I can have one or more "Reclamados" belonging to the same UF.由于“Reclamados”是不同的实体,我可以拥有一个或多个属于同一个 UF 的“Reclamados”。

That's where it fails!这就是它失败的地方!

If I have the same UF for two or more different "Reclamados", a error shows, as if, it was saving twice the same UF, although they're actually different, each one belonging to a different "Reclamado".如果我对两个或更多不同的“Reclamados”使用相同的 UF,则会显示错误,就好像它节省了两倍相同的 UF,尽管它们实际上不同,每个都属于不同的“Reclamado”。 It's not saving that same UF twice.它不是两次保存相同的 UF。 It saves one for each "Reclamado"它为每个“Reclamado”保存一个

An example of error is:错误的一个例子是:

"The instance of entity type 'UF' cannot be tracked because another instance with the key value '{Id: 26}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached." “无法跟踪实体类型 'UF' 的实例,因为另一个具有键值 '{Id: 26}' 的实例已被跟踪。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。 ”

How can I handle this?我该如何处理?

尽管如果您将持久性模型与实际业务代码分离可以避免这种情况,但要解决这个问题,您必须在 SaveChangesAsync 之前遍历所有 UF 并针对每个 UF 执行以下操作

context.Entry(uf).State = EntityState.Unchanged;

Your entry is already tracked by the context.您的条目已被上下文跟踪。 Just remove line context.FichasClientes.Update(ficha);只需删除行context.FichasClientes.Update(ficha); and your code will work.并且您的代码将起作用。

Update method is only useful when the entry is not tracking by the Context. Update方法仅在条目没有被上下文跟踪时才有用。 Read more about update 阅读有关更新的更多信息

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

相关问题 如何在两个对象之间创建连接,在 EF Core 中引用一个和多个 - How to create a connection between two objects, reference to one and to many in EF Core 保存具有相同对象属性 EF 核心的多个对象 - Saving multiple objects with same object property EF core EF Core不保存ParentId - EF Core not saving ParentId EF Core 3.0.0 - 两个相同类型的一对一关系 - EF Core 3.0.0 - Two one-to-one relations of same type 如何避免多个查询EF .net核心中的一个复杂对象 - How to avoid multiple queries one complex objects in EF .net core 两个连接ASP.NET Core、EF core、ArgumentNullException错误 - Two connections ASP.NET Core, EF core, ArgumentNullException error 关于保存相关实体的 EF 与 EF Core - EF vs EF Core on Saving Related Entities 如何在EF Core中将两个类映射到一个表 - How to map two classes to one table in EF Core 在 EF Core 3.1 中删除多行时出现错误“无法比较数组中的两个元素 - 至少一个 object 必须实现 IComparable” - Error "Failed to compare two elements in the array - At least one object must implement IComparable" for delete multiple row in EF Core 3.1 确定两个对象是否相等 - Determining if two objects are equal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM