Print Page | Close Window

Save entity thows me InvalidOperationException

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3528
Printed Date: 05-May-2025 at 2:16am


Topic: Save entity thows me InvalidOperationException
Posted By: pponzano
Subject: Save entity thows me InvalidOperationException
Date Posted: 09-Jul-2012 at 7:59am
Hello,
I've been facing this all day long... I cannot succed...

I've got an entity with n...m relation

Consider a Script table
a Variable Table
and a Script_Variable table (id_script int, id_variable int)

I've defined this UpdateScript inside my repository that does

public OperationResult UpdateScript(SCRIPT script, IEnumerable<VARIABILI> variabili, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)

       {

           entityManagerProvider.Manager.AttachEntity(script);

 

 

           script.SCRIPT_VARIABILI.Clear();

 

           if (variabili.Any())

           {

               variabili.ForEach((item) =>

               {

                   var scriptVariabili = entityManagerProvider.Manager.CreateEntity<SCRIPT_VARIABILI>();

 

                   scriptVariabili.ID_SCRIPT = script.ID;

                   scriptVariabili.ID_VARIABILE = item.ID;

                   entityManagerProvider.Manager.AttachEntity(scriptVariabili);

               });

           }

 

 

           return AlwaysCompletedOperationResult.Instance;

       }


I got this exception

An entity with this key: SCRIPT_VARIABILI: 1,0 already exists in this EntityManager on                  entityManagerProvider.Manager.AttachEntity(scriptVariabili);

I tougth that the script.SCRIPT_VARIABILI.Clear() would have removed all my pre-esistent items but it doesn't seem so... what am I doing wrong?

Thanks




Replies:
Posted By: sbelini
Date Posted: 09-Jul-2012 at 12:12pm
Hi Paolo,
 
Calling
script.SCRIPT_VARIABILI.Clear();
will not remove your scriptVariabili from the entity cache nor will it update their FK. (in your case the composite PK)
 
Also, you should be using AddEntity instead of AttachEntity.
 
Another thing to be aware of is that even if you delete a scriptVariabile and create a new one with the same Id, you will have the "An entity with this key: SCRIPT_VARIABILI: 1,0 already exists in this EntityManager" error message if both delete (of the old entity) and save (of the new entity) operations are performed in the same entityManager.SaveCh;anges call.
 
 
Regards,
   Silvio.
 


Posted By: pponzano
Date Posted: 09-Jul-2012 at 10:29pm
So what you suggest me to do Silvio?


Posted By: pponzano
Date Posted: 10-Jul-2012 at 2:45am
Hello Silvio.....I've almost managed but I really don't like this...hope you can give me guidelines to improve my code...
 
He'res the EndEdit method that will do the process
 

public IEnumerable<IResult> EndEdit(object sender, EditEndedEventArgs e)

      {

          switch (e.EditAction)

          {

              case Telerik.Windows.Controls.Data.DataForm.EditAction.Cancel:

                  repository.RejectChangesAsync();

 

                  break;

              case Telerik.Windows.Controls.Data.DataForm.EditAction.Commit:

                  var script = (sender as RadDataForm).CurrentItem as SCRIPT;

 

                  OperationResult operation;

 

                  var checkedItems = ListaVariabiliExt.Where(o1=>o1.IsChecked);

 

                  yield return (operation = repository.DeleteOldRelationsScriptVariabili(script, SelectedDataItem.SCRIPT_VARIABILI));

 

                  if (operation.CompletedSuccessfully)

                  {

                      yield return (operation = repository.SaveAsync());

 

                      if (operation.CompletedSuccessfully)

                      {

                          yield return (operation = repository.UpdateScript(script, checkedItems));

 

                          if (operation.CompletedSuccessfully)

                          {

                              yield return (operation = repository.SaveAsync());

                          }

                      }

                  }

 

                  if (operation.HasError)

                  {

                      //todo : add logging

                  }

 

 

                  break;

              default:

                  throw new InvalidOperationException("Edit action should be Cancel or Commit only.");

 

          }

          IsEditing = false;

          yield break;

 

      }

And here's the repository implementation
 
public OperationResult UpdateScript(SCRIPT script, IEnumerable<VARIABILI> variabili, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)
       {
           entityManagerProvider.Manager.AddEntity(script);
 
           if (variabili.Any())
           {
               variabili.ForEach((item) =>
               {
                    var scriptVariabili = entityManagerProvider.Manager.CreateEntity<SCRIPT_VARIABILI>();
 
                   scriptVariabili.ID_SCRIPT = script.ID;
                   scriptVariabili.ID_VARIABILE = item.ID;
 
                   entityManagerProvider.Manager.AddEntity(scriptVariabili);
               });
           }
           return AlwaysCompletedOperationResult.Instance;
       }
 
       public OperationResult DeleteOldRelationsScriptVariabili(SCRIPT script, RelatedEntityList<SCRIPT_VARIABILI> variabili, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)
       {
           if (variabili.Any())
           {
               for (int i = variabili.Count - 1; i >= 0; i--)
 
                   variabili.EntityAspect.Delete();
           }
 
           return AlwaysCompletedOperationResult.Instance;
       }

How can I improve this? all those nested yield return are not so good for me... and even for maintenability...
Thanks


Posted By: sbelini
Date Posted: 10-Jul-2012 at 8:37am
Hi Paolo,
 
I wouldn't be able to simply tell you what to improve without understanding the goal in dept.
 
We have a Professional Services team that would be able to better assist you in your task.
Please let me know if you would like to be contacted by them.
 
Regards,
   Silvio.



Print Page | Close Window