How to convert an insert to an update

Posts   
 
    
Philip
User
Posts: 17
Joined: 01-May-2009
# Posted on: 03-Nov-2009 16:57:38   

Hi there,

Using LLBLGen 1.6 Final, SelfServicing, with LinqADODS template in C# .Net 3.5

What I'm trying to accomplish seems logical, but I'm not sure how to approach it.

I have an entity called ItnEntity that I sometimes want to convert to an update in the middle of an insert. At the moment, I'm deleting an existing record if there is one already there with the same Itn value and then letting the insert continue, all in the OnSave() method:

In ItnEntity.OnSave()

var md = new LinqMetaData(Transaction);
var existingItnOnItn = md.Itn.SingleOrDefault(i => i.Itn == Itn);
if (existingItnOnItn != null)
{
    md.DeleteResource(existingItnOnItn);
    md.SaveChanges();
}

What I'd much rather do is just convert the current insert process to an update of the existing ItnEntity.

Is this possible? I was thinking that maybe using OnValidateEnitytBeforeSave() would work, I am not sure.

Thanks for your help! Philip

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 03-Nov-2009 20:45:18   

LLBLGen decides whether to update or insert based on the .IsNew property of the entity - you should be able to manually modify this to force the behaviour that you want.

Matt

Philip
User
Posts: 17
Joined: 01-May-2009
# Posted on: 04-Nov-2009 18:35:50   

Hi Matt,

I tried this out, replacing my delete code with IsNew = false; in OnSave().

I now get a unique index error from the db because it is still trying to do the insert. Maybe OnSave() is too late already and this has to be done somewhere else?

Thanks again, Philip

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Nov-2009 04:25:52   

Yes, is too late. You better do it outside. You don't want that behavior in all entities, do you?

David Elizondo | LLBLGen Support Team
Philip
User
Posts: 17
Joined: 01-May-2009
# Posted on: 06-Nov-2009 02:07:08   

Hey David, No, I don't want this behavior in all entities.

To really simplify, which is often good:

I have an entity called ItnEntity. It has three properties:

  1. ItnId - int
  2. Itn - string
  3. User - string

When an ItnEntity comes from the client with an Itn and User, but no ItnId, I have to check to see if that Itn already exists in the db.

If it doesn't, the record should just be inserted.

If it does, the existing record should be updated and the ItnEntity should be returned to the client with the existing ItnId, as if it had been an update.

So, I always check the in OnSave() to see if the Itn exists. At the moment, delete the existing record and then do an insert. However, this is not ideal. This should really become an update.

The rest of the entities in my system do not have this behavior.

Best, Philip

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Nov-2009 03:07:34   

Well, then try overriding the Save method, I think this is the right place to put your IsNew logic. This is done in a partial class of your special entity.

public override bool Save(IPredicate updateRestriction, bool recurse)
{
    // your logic to change the IsNew flag

    return base.Save(updateRestriction, recurse);
}
David Elizondo | LLBLGen Support Team