When I try to add a new item to my EntitySet, I'm getting this exception:
An entity with the same identity already exists in this EntitySet
However, when I inspect the EntitySet its count is 0.
Any ideas why I would be getting this error when the set is empty? How could an entity already exist in the set if the set has no items in it?
UPDATE
I've narrowed this down a bit more. This is only happening if I add the item to the set, remove it, then re-add it. Even tho the item isn't in the EntitySet any more it is still remembering it somehow. What do I need to do to make it forget?
UPDATE: Here are some code snippets for the classes and logic involved.
Server Entities:
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; }
}
Client side GetIdentity() for PhotoDestinationDto:
/// <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);
}
To remove photo destination client side:
PhotoDto photo = GetPhotoDto();
PhotoDestinationDto destToRemove = photo.Destinations.First(x => x.DropZoneId == 1);
photo.Destinations.Remove(destToRemove);
To add photo destination client side:
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.