jos*_*hls 3 silverlight silverlight-4.0 wcf-ria-services
当我尝试向我的EntitySet添加一个新项目时,我遇到了这个异常:
此EntitySet中已存在具有相同标识的实体
但是,当我检查EntitySet时,它的计数为0.
任何想法为什么我会在集合为空时收到此错误?如果集合中没有项目,那么集合中如何已存在实体?
UPDATE
我把它缩小了一点.只有在我将项目添加到集合中,删除它,然后重新添加它时才会发生这种情况.即使该项不在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; }
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
要删除照片目标客户端:
PhotoDto photo = GetPhotoDto();
PhotoDestinationDto destToRemove = photo.Destinations.First(x => x.DropZoneId == 1);
photo.Destinations.Remove(destToRemove);
Run Code Online (Sandbox Code Playgroud)
要添加照片目标客户端:
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.
Run Code Online (Sandbox Code Playgroud)
根据你的代码Unit = selectedUnit和p.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
Run Code Online (Sandbox Code Playgroud)
好:
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");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5555 次 |
| 最近记录: |