none
Valid object fails on SubmitChanges because of previous failure? RRS feed

  • Question

  • I hope this makes sense:

    I have some code that looks like this:

    public void Save()
    {
     . . . <snip> . . .
    if (tblActivity.ActivityId == 0) _db.TblActivityMsts.InsertOnSubmit(tblActivity);
    _db.SubmitChanges();
     . . . <snip> . . .
     }

    However, the tblActivity object is not valid in the first call (I discovered this via unit testing). Then, the object is made valid and the function is called again. The second time the Save() function is called, the tblActivity is populated with valid values but the _db.SubmitChanges() throws an exception claiming that a particular property cannot be null. That particular property was null the first time but not the second time.

    OK, so _db or _db.TblActivityMst is hanging on to that object, I guess.

    How do I clear it? I'm assuming that I should catch the exception and do something to prevent that bad object from being considered for submission again. How do I do that? Wrap it in a transaction? Call _db.Refresh()? (I tried this, but it didn't seem to work as desired).

    Any help is appreciated.

    Thanks,

    Vic

     

    Monday, May 3, 2010 8:38 PM

Answers

  • It does seem you need to detach the object from the disconnected context before re-saving it. I'm assuming you have created tblActivity prior to calling Save() and it is in a higher scope than the save() method....silly OO langauges =P.

    I have a suggestion for you though, rather than constantly worrying about which context your entity is bound to, if your certain that the entity is ready to be saved and the reasons for failure are a nuisance and not important....then you may want to write your Save() method to call a T-SQL stored procedure..

    This procedure can define several parameters and insert the data into the correct table...here is a blog with some detail on what I'm talking about...

     

    http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

     

     

     

    Tuesday, May 4, 2010 5:05 AM

All replies

  • It does seem you need to detach the object from the disconnected context before re-saving it. I'm assuming you have created tblActivity prior to calling Save() and it is in a higher scope than the save() method....silly OO langauges =P.

    I have a suggestion for you though, rather than constantly worrying about which context your entity is bound to, if your certain that the entity is ready to be saved and the reasons for failure are a nuisance and not important....then you may want to write your Save() method to call a T-SQL stored procedure..

    This procedure can define several parameters and insert the data into the correct table...here is a blog with some detail on what I'm talking about...

     

    http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

     

     

     

    Tuesday, May 4, 2010 5:05 AM
  • It's a little late to change our design to use stored procs for all of the CRUD work, but you did get me pointed in the right direction to solving this problem.

    Actually, the tblActivity is instantiated in the Save() function, but the context (_db) is a class-level variable. My problem was resolved by simply creating a new instance of my context in the Exception catch . . .

    _db = new DBCONTEXT(Utility.ConnectionString);

    Obviously, this solution may not work in all situations, but for me, the context is confined to the individual business class and only processes one (sometimes a few) changes at a time.

    I'm open to suggestions on "better" ways to solve this problem, if anyone has any.

    Thanks,

    Vic

    Tuesday, May 4, 2010 2:42 PM