public void Update(SChannels entity, List<ScsgLink> deletedLinks) { try { EntityState entityState = _siriusContext.GetEntityState(entity); if (entityState == EntityState.Detached) { _dbSet.Attach(entity); _siriusContext.SetModified(entity); foreach(ScsgLink link in deletedLinks) { _siriusContext.SetDeleted(link); } foreach(ScsgLink link in entity.ScsgLink.Where(s=>s.Added==true)) { _siriusContext.SetAdd(link); } } SaveChanges(); }So, I can pass the list of deleted links and set them properly to deleted status and it seems to work.
EntityState entityState = _siriusContext.GetEntityState(entity); if (entityState != EntityState.Deleted) { foreach (ScsgLink link in entity.ScsgLink) { if (link.ScsgLinkId != 0) { _siriusContext.SetDeleted(link); } } _siriusContext.SetDeleted(entity);Here the first iteration works, but the second throws an exception that collection is being modified. I am wondering why is that if I'm only setting the deleted status, I don't believe it actually removes the item from collection until I run the SaveChanges method.
public void SetDeleted(object entity) { Entry(entity).State = EntityState.Deleted; }Thanks in advance.
public override void Delete(SChannels entity) { if (RelatedMax4SalesExist(entity.ChannelId)) { throw new DeleteTriggerException(Messages.cannotDeleteException + " There are existing Max4Sale Rows for this channel Id = " + entity.ChannelId.ToString()); } EntityState entityState = _siriusContext.GetEntityState(entity); if (entityState != EntityState.Deleted) { // foreach (ScsgLink link in entity.ScsgLink) // { // if (link.ScsgLinkId != 0) // { // _siriusContext.SetDeleted(link); // } // } // _siriusContext.SetDeleted(entity); //} //else //{ //http://stackoverflow.com/questions/16194162/entitystate-deleted-does-not-work-removeentity-does _dbSet.Attach(entity); _dbSet.Remove(entity); } try { SaveChanges(); } catch (DbUpdateException ex) { ThrowDeleteError(ex); } }and it worked fine - deleted the related entities.