Because it was not detected in the ObjectStateManager, the object could not be destroyed.

asp.net-mvc entity-framework

Question

I have anything along these lines:

public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }

I now have a exception: The object could not be destroyed because the ObjectStateManager could not locate it. Upon attempting to add the entity to using AttachTo objectContext (), I receive:

The ObjectStateManager cannot monitor more than one object with the same key since another object with the same key already exists in the ObjectStateManager.

Why is this bad?

Example:

namespace CAFM.Data.Repository
{
    public abstract class RepositoryBase<T> : IRepository<T>
        where T : EntityObject
    {
        public RepositoryBase()
            : this(new CAFMRepositoryContext())
        {
        }
        static RepositoryBase()
        {
        }
        public RepositoryBase(IRepositoryContext repositoryContext)
        {
            _context = repositoryContext ?? new CAFMRepositoryContext();
            _entity = _repositoryContext.ObjectContext.CreateObjectSet<T>();
        }
        private readonly ObjectContext _context;
        private readonly ObjectSet<T> _entity;
        protected ObjectContext Context
        {
            get { return _context; }
        }
        protected IObjectSet<T> Entity
        {
            get { return _entity; }
        }



        #region IRepository Members

        private string GetEntityName()
        {
            return string.Format("{0}.{1}", _entity.EntitySet.EntityContainer, _entity.EntitySet.Name);
        }
        public T Add(T entity)
        {
            var fqen = GetEntityName();
            Context.AddObject(fqen, entity);
            Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            return entity;
        }
        public T Update(T entity)
        {
            Context.ApplyCurrentValues(GetEntityName(), entity);
            Context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
            return entity;
        }
        public void Delete(T entity)
        {
            Context.DeleteObject(entity);
            Context.SaveChanges();
        }
        #endregion
    }
}
1
2
6/7/2011 10:05:11 AM

Accepted Answer

The entity you want to remove must first be fetched from your context. It is best to carry out this task using a main key comparison. It could like this, although I'm not familiar with the object hierarchy ofTabMaster and TabMasterViewModel the names of the attributes can be incorrect.

public void Delete(TabMasterViewModel entity) { 
    TabMaster des = _tabmasterRepository.FirstOrDefault( e.Id = entity.ID );
    if (des != null) {
        _tabmasterRepository.Delete(des); 
    }
} 

The values from your view model have been translated to a new Entity that you have built. However, since the context is unaware of the object, he was unable to remove it.

12
12/23/2012 8:29:18 PM

Popular Answer

You might just "Attach" the object in that manner to the current context:

public void Delete(T entity)
{
    context.AttachTo(entity.EntityKey.EntitySetName, entity);
    Context.DeleteObject(entity);
    Context.SaveChanges();
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow