Copy Entity to New Entity

Posts   
 
    
Valdar
User
Posts: 24
Joined: 07-Oct-2005
# Posted on: 31-Oct-2006 23:06:42   

Hello,

I have a collection that I would like to keep all the data for, but only set the IsNew property of the entities to true so they can be re-inserted into the database.

But when I set the IsNew property to true it resets all the values of the entity.

Is there a way to accomplish this with ajust a few lines of code? Having to copy over each value of an entity to a new entity could be very tedious because some of my entities have around 40 columns.

Thanks for any help on this, have to run so I wish I could write more, will do that when I get back.

Valdar
User
Posts: 24
Joined: 07-Oct-2005
# Posted on: 01-Nov-2006 05:15:22   

Alright, I have an invoice with invoicelineitems and when an invoice is saved, I have no way to tell if an entity has been deleted from the collection using LLBLGen.

So I need to delete all invoicelineitems for this invoice and re-insert the ones I have in my collection (which may or may not be new).

Does anyone know how this would be accomplished?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 01-Nov-2006 06:45:35   

So is the second a clarification of the first request? Why don't you know what entities have been removed from the collection? Could you possibly post a bit more about exactly what the scenario is? Thanks.

Valdar
User
Posts: 24
Joined: 07-Oct-2005
# Posted on: 01-Nov-2006 20:41:46   

Yeah, the 2nd post was a clarification of the first.

So I store my entities in my ViewState while they are being worked on then when the user clicks "Save" I save the entity.

I have an Invoice entity that has a 1:n relationship with invoicelineitems.

So in the course of interacting with the web page the user may remove some of the invoicelineitems.

Then when the user clicks "Save" I do not know which entities have been removed so I just delete all the current invoicelineitems and re-insert the ones my Invoice.InvoiceLineITem collection has, but not all of the InvoiceLineItemEntity have their IsNew = true (because they might have been retrieved when the Invoice was first retrieved). Because of this the entities with IsNEw = false are not inserted in to the database.

So, I need a way to declare that all entities in the collection should be inserted new. Right now, when I set InvoiceLineItemEntity.Isnew = true it resets all the fields of the InvoiceLineItem entity.

I hope that all made sense.

Valdar
User
Posts: 24
Joined: 07-Oct-2005
# Posted on: 01-Nov-2006 23:12:31   

I solved the issue, but not in a way that I like much. Here is what I did:

foreach(InvoiceLineItemEntity oInvoiceLineItem in oInvoice.InvoiceLineItem) { string sInvoiceId = oInvoiceLineItem.InvoiceId; string sItemNumber = oInvoiceLineItem.ItemNumber; long lQuantity = oInvoiceLineItem.Quantity; oInvoiceLineItem.IsNew = true; oInvoiceLineItem.InvoiceId = sInvoiceId; oInvoiceLineItem.ItemNumber = sItemNumber; oInvoiceLineItem.Quantity = lQuantity; }

RelationPredicateBucket filter = new RelationPredicateBucket(); filter.PredicateExpression.Add(InvoiceLineItemFields.InvoiceId == oInvoice.Id); adapter.DeleteEntitiesDirectly("InvoiceLineItemEntity", filter); adapter.SaveEntity(oInvoice, true, true);

Unfortunately, my InvoiceLineItem entitiy has about 40 columns of various information so this is a lot of typing. I have a few other scenarios where this comes up in our CRM app (Notes have multiple customers, multiple order #'s, multiple files, etc.).

It works for now, but I thought there would be a cleaner way to do it.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-Nov-2006 07:25:55   

You should set the IsNew to true, and loop on the fields of the entity and set each field's IsDirty = true.

Speaking about the way you areimplementing this, you may try to use a UnitfWork object. Which you can store in the viewstate till the user hits Save.

With a UOW you can add entities for Delete, entities for Save. And you get to commit all the actions at one call afterwards, by Commiting the UOW.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 02-Nov-2006 08:18:34   

UOW is the best idea. However, I use the method below and am successfully copying entities. Create a new entity and pass it in as the destination entity along with the source entity.

    private void CopyFieldsInEntity(EntityBase srcEntity, EntityBase destEntity)
    {
        if (!srcEntity.IsNew)
            destEntity.Fields = (IEntityFields)((EntityFields)srcEntity.Fields).Clone();

        if (srcEntity.IsNew || srcEntity.IsDirty)
        {
            foreach (EntityField field in (EntityFields)srcEntity.Fields)
            {
                if (field.IsChanged)
                    destEntity.SetNewFieldValue(field.FieldIndex, field.CurrentValue);
            }
        }
    }

If the source entity has collections in it you wish to copy over, you must do that also.


SomeOrderEntity newOrder = new SomeOrderEntity();
CopyFieldsInEntity(currentOrder, newOrder);
foreach (EntityBase item in currentOrder.OrderDetailLines)
{
    EntityBase newItem = (EntityBase)newOrder.OrderDetailLines.AddNew();
    CopyFieldsInEntity(item, newItem)
}

And so forth. You might need to change this a bit to mark them as new or dirty when you copy them no matter what...