Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Modifying collection
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Modifying collection
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01626533
Message ID:
01626533
Vues:
31
Hi everybody,

In the Update method I have the following code that works fine:
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.

However, I'm trying to implement a similar code in the Delete method to first delete related links:
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.

So, why I am getting the error and how should I change the code to avoid it?

The SetDeleted method is simply:
public void SetDeleted(object entity)
        {
            Entry(entity).State = EntityState.Deleted;
        }
Thanks in advance.

UPDATE. According to http://stackoverflow.com/questions/16194162/entitystate-deleted-does-not-work-removeentity-does the Attach/Remove method works correctly when cascade relationship is specified (as in our case), so I've changed the code to
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.
If it's not broken, fix it until it is.


My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform