Getting "An entity with the same identifier already exists in this EntitySet", but the EntitySet is empty

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; }

    /* 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; }
}

GetIdentity() 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);
}

:

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.
+3
6

Unit = selectedUnit p.Destinations.Add(dest);, , , . .

, . , , New, . .


, , , .

, , , .

:

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");
+6

, . EntityCollection ( ) - , FK . , photo.Destinations.Remove() DomainContext (ctxt.PhotoDestinationDtos EntitySet). .

, ctxt.PhotoDestinationDtos.Remove.

+3

, , , "0", , , 0 , , , , Inser, DomainService,

+1

, , , . , , , .

+1

, PK/identity, db , ? OnError DomainService, . ( /Silverlight, ?)

0

EntitySet, SubmitChanges(). - . delete.

EntitySet ( node ), _IdentityCache. EntitySet...

enter image description here

... _identityCache .

enter image description here

, EntitySet, EntityState of Deleted, .

entitySet.EntityContainer.GetChanges().RemovedEntities

RemovedEntities , .

0

Source: https://habr.com/ru/post/1745004/


All Articles