gpt4 book ai didi

silverlight - 获取 "An entity with the same identity already exists in this EntitySet"但 EntitySet 为空

转载 作者:行者123 更新时间:2023-12-04 03:24:30 25 4
gpt4 key购买 nike

当我尝试向 EntitySet 添加新项目时,出现以下异常:

An entity with the same identity already exists in this EntitySet



但是,当我检查 EntitySet 时,它的计数为 0。

任何想法为什么我会在集合为空时收到此错误?如果集合中没有项目,实体怎么可能已经存在于集合中?

更新

我已经缩小了一点。仅当我将项目添加到集合中,删除它,然后重新添加它时才会发生这种情况。即使该项目不再在 EntitySet 中,它仍然以某种方式记住它。我需要做什么才能让它忘记?

更新:以下是所涉及的类和逻辑的一些代码片段。

服务器实体:
public class PhotoDto
{
[Key]
[Editable(false)]
public int Id { get; set; }

/* clip */

[Include]
[Association("Photo_Destination", "Id", "PhotoId")]
public EntitySet<PhotoDestinationDto> Destinations { get; set; }
}

public class PhotoDestinationDto : BaseDestionationDto
{
[Key]
[Editable(false, AllowInitialValue = true)]
public int PhotoId { get; set; }

[Key]
[Editable(false, AllowInitialValue = true)]
public bool IsAnnotated { get; set; }

[Key]
[Editable(false, AllowInitialValue = true)]
public int DropZoneId { get; set; }
}

public class BaseDestinationDto
{
[Key]
[Editable(false, AllowInitialValue = true)]
public Guid DatabaseUid { get; set; }

[Key]
[Editable(false, AllowInitialValue = true)]
public string Unit { get; set; }

[Key]
[Editable(false, AllowInitialValue = true)]
public string EqCircId { get; set; }

[Key]
[Editable(false, AllowInitialValue = true)]
public string EqType { get; set; }
}

PhotoDestinationDto 的客户端 GetIdentity():
/// <summary>
/// Computes a value from the key fields that uniquely identifies this entity instance.
/// </summary>
/// <returns>An object instance that uniquely identifies this entity instance.</returns>
public override object GetIdentity()
{
if ((((this._eqCircId == null)
|| (this._eqType == null))
|| (this._unit == null)))
{
return null;
}
return EntityKey.Create(this._dropZoneId, this._eqCircId, this._eqType, this._isAnnotated, this._photoId, this._unit, this._databaseUid);
}

要删除照片目标客户端:
PhotoDto photo = GetPhotoDto();
PhotoDestinationDto destToRemove = photo.Destinations.First(x => x.DropZoneId == 1);
photo.Destinations.Remove(destToRemove);

添加照片目的地客户端:
var dest = new PhotoDestinationDto
{
DropZoneId = zoneId,
EqCircId = selectedEqCircId,
EqType = selectedEqType,
Unit = selectedUnit,
PhotoId = id,
DatabaseUid = selectedDatabaseId
};

p.Destinations.Add(dest); // this is where exception is thrown. p.Destinations.Count is 0 here.

最佳答案

看你的代码 Unit = selectedUnitp.Destinations.Add(dest);和您描述的其他症状似乎完全相同。我下面的发现为我解决了这个问题。

如果您因此而在数据库中发现任何重复的实体,也很感兴趣。我得到了之前已经保存的行神奇地转向 New然后成为欺骗记录。下面这个简单的更改再次修复了它。

我发现有一个竞争条件,如果您创建一个实体并在将父实体添加到其实体集之前立即为其分配外键关系,则会出现此错误。

事实证明,设置外键关系的行为实际上将实体添加到其相应的实体集 - 但有时会导致竞争条件,尤其是当您的 UI 正在加载、保存和清除实体集时。

坏:

var todo = new TODO();
todo.Status = TODOContext.Statuses.Single(x=>x.Status == "Unknown");
todo.AssignedTo = TODOContext.Users.Single(x=>x.UserName == "JSMITH");
TODOContext.TODOs.Add(todo); // adding entity after setting FK

好:
var todo = new TODO();
TODOContext.TODOs.Add(todo); // add entity before setting FK
todo.Status = TODOContext.Statuses.Single(x=>x.Status == "Unknown");
todo.AssignedTo = TODOContext.Users.Single(x=>x.UserName == "JSMITH");

关于silverlight - 获取 "An entity with the same identity already exists in this EntitySet"但 EntitySet 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2814250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com