Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Error when trying to save the second time
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Titre:
Error when trying to save the second time
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01651804
Message ID:
01651804
Vues:
211
UPDATE. Now reading https://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent and will be trying ideas from it.

Hi everybody,

I'm getting the following error:

System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Attaching an entity of type 'Siriusware.Models.Prefs' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation)
       at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
       at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassa.<Attach>b__9()
       at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
       at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
       at System.Data.Entity.DbSet`1.Attach(TEntity entity)
       at SysManager.Repository.RepositoryBaseWithList`2.Update(T entity) in D:\_Siriusware\Main\SysManager\SysManager.Repository\RepositoryBaseWithList.cs:line 158
       at SysManager.Adapter.PreferencesAdapter.Update(Prefs prefs) in D:\_Siriusware\Main\SysManager\SysManager.Adapter\PreferencesAdapter.cs:line 155
       at SysManager.Web.Areas.AdmissionControl.Controllers.AccessApiController.UpdatePrefs(EditPrefsViewModel prefsViewModel) in D:\_Siriusware\Main\SysManager\SysManager.Web\Areas\AdmissionControl\Controllers\AccessApiController.cs:line 253
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
  InnerException:
-----------------------------------

when I try to click the 'Save' button the second time (and I didn't change anything). I'm trying to figure out what should I do to fix it?

Our base repository code is the following:
 public virtual void Update(T entity)
        {
            try
            {
                EntityState entityState = _siriusContext.GetEntityState(entity);
                if (entityState == EntityState.Detached)
                {
                    _dbSet.Attach(entity);
                    _siriusContext.SetModified(entity);
                }               

                SaveChanges();
            }

    //catch here
}
The error happens on the Attach line. I was thinking it may be due to not re-loading the model data after saving, but I added that code into script controller and still get the same error, so I'm not sure how to fix it now.

Thanks a lot in advance.
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