Is there any way to do a Insert or Update / Merge / Upsert in LLBLGen

2.2k Views Asked by At

I'd like to do an upmerge using LLBLGen without first fetching then saving the entity.
I already found the possibility to update without fetching the entity first, but then I have to know it is already there.

Updating entries would be about as often as inserting a new entry.

Is there a possibility to do this in one step?
Would it make sense to do it in one step?

Facts:

  • LLBLgen Pro 2.6
  • SQL Server 2008 R2
  • .NET 3.5 SP1
3

There are 3 best solutions below

1
On

As far as I know, this is not possible, and could not be possible.

If you were to just call adapter.Save(entity) on an entity that was not fetched, the framework would assume it was new. If you think about it, how could the framework know whether to emit an UPDATE or an INSERT statement? No matter what, something somewhere would have to query the database to see if the row exists.

It would not be too difficult to create something that did this more or less automatically for single entity (non-recursive) saves. The steps would be something like:

  1. Create a new entity and set it's fields.
  2. Attempt to fetch an entity of the same type using the PK or a unique constraint (there are other options as well, but none as uniform)
  3. If the fetch fails, just save the new entity (INSERT)
  4. If the fetch succeeds, map the fields of the created entity to the fields of the fetched entity.
  5. Save the fetched entity (UPDATE).
5
On

I know I'm a little late for this, but As I remember working with LLBLGenPro, it is totally possible and one of its beauties is everithing is possible! I don't have my samples, but I'm pretty sure you there is a method named UpdateEntitiesDirectly that can be used like this:

// suppose we have Product and Order Entities
using (var daa = new DataAccessAdapter())
{
    int numberOfUpdatedEntities = 
        daa.UpdateEntitiesDirectly(OrderFields.ProductId == 23 && OrderFields.Date > DateTime.Now.AddDays(-2));
}

When using LLBLGenPro we were able to do pretty everything that is possible with an ORM framework, it's just great! It also has a method to do a batch delete called DeleteEntitiesDirectly that may be usefull in scenarios that you need to delete an etity and replace it with another one.

Hope this is helpful.

3
On

I think you can achieve what you're looking for by using EntityCollection. First fetch the entities you want to update by FetchEntityCollection method of DataAccessAdapter then, change anything you want in that collection, insert new entities to it and save it using DataAccessAdapter, SaveCollection method. this way existing entities would be updated and new ones would be inserted to the Database. For example in a product order senario in which you want to manipulate orders of a specified product then you can use something like this:

int productId = 23;
var orders = new EntityCollection<OrderEntity>();
using (DataAccessAdapter daa = new DataAccessAdapter())
{
    daa.FetchEntityCollection(orders, new RelationPredicateBucket(OrderFields.ProductId == productId))

    foreach(var order in orders)
    {
        order.State = 1;
    }

    OrderEntity newOrder = new OrderEntity();
    newOrder.ProductId == productId;
    newOrder.State = 0;
    orders.Add(newOrder);

    daa.SaveEntityCollection(orders);
 }