When I try to add a new element to my EntitySet, I get this exception:
An object with the same identifier already exists in this EntitySet
However, when I check the EntitySet, its number is 0.
Any ideas why I get this error when the set is empty? How does an object already exist in a set if there are no elements in the set?
UPDATE
I narrowed it down a bit more. This only happens if I add an item to the collection, delete it, and then add it again. Even if the item is not already in the EntitySet, it still remembers it. What do I need to do to make him forget?
UPDATE: The following are code snippets for the classes and logic involved.
Server objects:
public class PhotoDto
{
[Key]
[Editable(false)]
public int Id { get; set; }
[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; }
}
GetIdentity() PhotoDestinationDto:
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.