Out of Sync after Update using Adapter Model

Posts   
 
    
SethInSB
User
Posts: 3
Joined: 13-Jun-2006
# Posted on: 13-Jun-2006 03:11:30   

When updating an Entity without having fetched it, I would expect that the columns/attributes you have saved would be flagged as "synchronized" since you just updated them. However, if you try to use a field which you saved, you get a not-synchronized error.

Example Offending Code:


  BookEntity book = new BookEntity();
  book.Id = 27; //known book id existing in database; no need to fetch this entity to update fields
  book.Title = "The Sphere";
  book.Author = "Michael Chrichton";
  Adapter.SaveEntity(et);

MessageBox.Show("You just saved the following book: " + book.Title);

you will get an exception when trying to use the book.Title field because the entity is not synchronized.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 13-Jun-2006 03:24:48   

Change

Adapter.SaveEntity(et);

to

Adapter.SaveEntity(et, True);

Which sets the RefetchAfterSave flag, keeping it in sync.

SethInSB
User
Posts: 3
Joined: 13-Jun-2006
# Posted on: 13-Jun-2006 04:14:15   

I would do that, but I don't want to hit the database again and cause performance loss.

I still consider this a bug that is easily fixable.

You just updated the database with a value, you KNOW that value is sychronized.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Jun-2006 06:14:36   

You may have done it like that:

BookEntity book = new BookEntity();
book.Id = 27; //known book id existing in database; no need to fetch this entity to update fields
book.Title = "The Sphere";
string title = book.Title;
book.Author = "Michael Chrichton";
Adapter.SaveEntity(et);

MessageBox.Show("You just saved the following book: " + title);

The problem is that when you save an entity, many things could happen in the database, that the code can't be aware of, such as an identity field being set, or one or more triggers changing some fields values, you fieldr could have been altered by a trigger or so.

That's why an out of Sync exception is thrown when you use an entity that's not refetched.

SethInSB
User
Posts: 3
Joined: 13-Jun-2006
# Posted on: 13-Jun-2006 21:10:18   

I suppose this makes sense. I guess LlblGen can't be an entire god over the SQL state and be aware of triggers.

Your posted code is exactly what I ended up doing, thanks for the clarification. Also, I suppose that if I really was concerned about efficiency enough to not refetch I'd just be calling a stored procedure directly and not even using llblgen in the first place.

Gotta stop trying to make every tool I get into a swiss knife, that gimps it too much and using the real tools on their own always gives better results.

I'm still learning this llblgen tool, thank you very much for the help.

mikeg22
User
Posts: 411
Joined: 30-Jun-2005
# Posted on: 14-Jun-2006 02:33:57   

It would be nice if LLBLGEN gave the option of requiring a refetch for the entity to be in sync. For example, as a rule, we don't use database triggers in this way. For these setups, it would be nice not to have to refetch on every save, as the refetch causes a performance hit on mass updates (we have to access these entities after the update for auditing reasons).

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 14-Jun-2006 05:57:59   

Hi,

If you want to use an entity after it have been saved without a refetch you can set the entity.fields.state = fetched.