0

I have this C# code:

 public object guardardinerohoy(float dinero,string comentario)
 {
    object dineromov1 = this.nuevodineromovimiento(dinero, variablesestaticas.usuarioglobal, DateTime.Now, null, claseenumeraciones.enumdineromovimiento.iniciosistema, comentario, DateTime .Now );
    object resultado = "ok";
    string abrirconexion = Conexion.conexion.abrirconexion();
    if (dineromov1.GetType() != "".GetType() && abrirconexion == "ok")
    try
    {
       Conexion.conexion.conect.AddTodineromovimiento((dineromovimiento)dineromov1);
       Conexion.conexion.conect.SaveChanges();
       return "ok";
    }

    catch (Exception ex)
    {
       resultado = ex.Message;
    }

    else
    {
       resultado = dineromov1.ToString() + abrirconexion;
       return resultado;
    }
  }

I return "ok" if this saved successfully. Now when I checked if this was saved it was not. I do not understand why if it did not return an exception. This does not happen all the time. Sometimes it saves and sometime it does not.

I found this thread which says if it does not have exception, everything is ok.

Check if an insert or update was successful in Entity Framework

Community
  • 1
  • 1
angel
  • 4,474
  • 12
  • 57
  • 89

1 Answers1

0

Entity Framework will throw an exception upon failure of Insert, Update or Delete. Thus, you can assume with no exception that it's successful.

Gabe
  • 49,577
  • 28
  • 142
  • 181
  • well, i do not got exception, but when i check if this saved, this was not saved, now if i debug it (slow) it saved, but if i debug it and i let it continue(f5) it does not save but it does not return exception. – angel Apr 23 '13 at 21:24
  • As he says, if it does not throws and exception then all was saved. Modified entities saves and if you want to save new ones you have to do like you did context.AddToEntity(entity). Check if it´s catching the exception. Also, where are you looking if you are saving the data? Because it can be dumb, but if you are using the Managment Studio, you have to refresh the window.. – Andres Apr 23 '13 at 21:36
  • ok, i am not sure but i believe this is overwritting the database.sdf file. – angel Apr 23 '13 at 22:13
  • Try generating a new database from your ADO entity model. And check if it works.. – Andres Apr 23 '13 at 22:32
  • i do not understand whats happening, but sometimes all data only 1 table is deleted, other tables are not deleted, and i never use delete only updates and status for know if this was "delete" – angel Apr 23 '13 at 22:54
  • now i do, i save, and after i return this i have added, and check if this is not null then, it is not null!! now when i execute again this is not :s whats happening, BUT, if i do the same, and i open the client catalog >see sales by customer then, the first is saved right. – angel Apr 23 '13 at 23:05