0

When i use NHibernate to update or save(insert) entity,i find the code (session.Flush())doesn't work and no error message,and the program is end in there(not breakdown,function is end in there). But i was success to use it insert entity and i didn't modify any code. I'm confused,pls help me. The code is there:

ISession session = SessionBuilder.SessionFactory.OpenSession();

        switch (editType)
        {
            case 'S': {
                    try
                    {
                        //new info set default;
                        cs.ID = 132;
                        clc.ID = cs.ID;
                        cmc.ID = cs.ID;
                        cs.COEFFICIENT_ID = 0;
                        cs.IF_ASSOCIATION = 'N';
                        cs.COST_PRICE = 0.0f;

                        session.Save(cs);
                        session.Save(clc);
                        session.Save(cmc);
                        session.Flush();//<<--if i set a breakpoint in there,
                          //and next step the whole function will end,no catch no finally
                          //no return,the application seem never run this function.
                          //and the application continues to run normal.

                        flg = true;
                    }catch (Exception e)
                    {
                        flg = false;
                        throw e;
                    }
                    finally
                    {
                        session.Close();
                    }
                    break;
                }
            case 'E': {
                    try
                    {
                        session.Update(cs);
                        session.Update(clc);
                        session.Update(cmc);
                        session.Flush();//<<--if i set a breakpoint in there,
                          //and next step the whole function will end,no catch no finally
                          //no return,the application seem never run this function.
                          //and the application continues to run normal.
                        flg = true;
                    }catch(Exception e)
                    {
                        flg = false;
                        throw e;
                    }
                    finally
                    {
                        session.Close();
                    }

                    break; }
            default: { return false; }
        }
        return flg;
    }
heiheihei
  • 659
  • 1
  • 6
  • 15
  • In reference to the exception thing mentioned below, I would suspect that you catch an hide the exception somewhere not shown in your example - at a higher level. Also, when rethrowing an exception, beware the difference between `throw e;` and just `throw;`: If you leave out the exception variable, it will preserve (and extend) the original stacktrace, which is usually what one wants. – Oskar Berggren Jun 02 '18 at 11:08

2 Answers2

0

Instead of Session.Flush() try to use a transaction:

using (var transaction = session.BeginTransaction())
{
    transaction.Commit();
}
core
  • 851
  • 1
  • 8
  • 28
  • eh,it works,thanks very much. but why.... I'm going to be driven crazy by session.flush() – heiheihei May 12 '18 at 09:00
  • Maybe this will guide you in the right direction: https://stackoverflow.com/questions/43320/nhibernate-isession-flush-where-and-when-to-use-it-and-why?rq=1 And please: if it is working please mark the answer as the accepted answer. – core May 12 '18 at 09:08
  • eh.....the same problem,now is transaction.Commit() is doesn't work.... – heiheihei May 12 '18 at 09:12
  • Eh,I find out where is the problem. The string is beyond the range,i change the length and the work normal. – heiheihei May 12 '18 at 09:23
0

eh,the problem is that the some strings are beyond the limit length of database-type. But,i'm confused why the VS can't catch the error and seem never happened?

heiheihei
  • 659
  • 1
  • 6
  • 15