Writing entities directly to the database

Posts   
 
    
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 27-May-2006 11:18:12   

Hi,

I'm binding entities to a grid (Janus v3) and I want to persist each change (adding, deleting and changing) without any buttons just when the user changes the row.

I managed the delete with the EntityRemoving Event.


void myEntityCollection_EntityRemoving(object sender, CancelableCollectionChangedEventArgs e)
{
  int delId = ((myEntity) e.InvolvedEntity).ID;
  IPredicateExpression delFilter = new PredicateExpression((myEntityFields.ID == delId));
  myEntityCollection.DeleteMulti(delFilter);
}

But it only works, if I leave myEntityCollection.AllowRemove to false. If I set it to true, I get an Exception.

The edit I have achieved using the CurrentItemChanged event of the binding source. In that event I test the myEntityCollection.DirtyEntities property and call Save for each entity in that list. This also works for new entities but I'm having doubts whether this is a good way. I thought the new event EntityAdded would be the right place for the adding, but calling SaveMulti in it doesn't work. The added entity is still empty in that event.

Could you please enlighten me what would be the right way for this scenario.

Thanks in advance

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 27-May-2006 18:34:33   

What exception do you get? (with stacktrace!)

Frans Bouma | Lead developer LLBLGen Pro
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 27-May-2006 20:19:43   

Sorry, seems to be my fault. I've worked on the program today and now the exception is gone. (although the code in EntityRemoving is still the same) confused It now behaves the same, no matter which value the AllowRemove property has.

So the code is working now as expected. Anyway I would be glad if you could tell me whether you would recommend the same way to design this scenario.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 29-May-2006 10:42:12   

The thing with these situations is that an edit is in progress and you want to delete the entity. This can cause problems as the edit cycle has to end on an existing entity. This is always the case, as a grid starts an edit cycle when you enter a cell.

So it's best to use EntityRemoved instead of EntityRemoving, as you then know the entity is removed from the collection and you can take action, e.g. add it to a unitofwork, or delete it directly by calling its Delete() method.

Frans Bouma | Lead developer LLBLGen Pro
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 29-May-2006 13:06:48   

Thanks for that, I will change it to EntityRemoved.

One last question: why is EntityAdded not working the same. I would assume, that the entity is already added in this event and I can access all fields of it. But even though there is a new entity (and it's IsNew property is true), all other properties are empty, so I can't save it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 29-May-2006 14:07:56   

teizela wrote:

Thanks for that, I will change it to EntityRemoved.

One last question: why is EntityAdded not working the same. I would assume, that the entity is already added in this event and I can access all fields of it. But even though there is a new entity (and it's IsNew property is true), all other properties are empty, so I can't save it.

In which situation does this occur? because EntityAdded is called as the very last thing in the Add() method. So if you fetch data from the db, the data is first stored in the entity, then the entity is added to the collection.

Frans Bouma | Lead developer LLBLGen Pro
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 29-May-2006 21:15:47   

The entity is not read from the database, I create it in the grid. I add a new row there and if I change the row, EntityAdded is called. In that event I have access to a new entity (Collection.Count is incremented, IsNew of the last entry is true), but all fields of this entity are empty, just as if I had called new Entity(). If they'd be filled, I could simply call the Save-method in EntityAdded to persist the new entry.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 29-May-2006 21:32:03   

But you're creating a new entity, so how would these fields be filled then? (or better: why do you expect the fields to be filled? ) That's what I don't understand simple_smile , so if you would explain that to me, it would be great simple_smile

Frans Bouma | Lead developer LLBLGen Pro