SaveMulti and bypass ORMConcurrencyException

Posts   
 
    
sunnyman
User
Posts: 51
Joined: 21-Feb-2007
# Posted on: 01-May-2007 04:23:09   

I want SaveMulti to success even if some entities is deleted by another user form db, in normal cases it will throw ORMConcurrencyException.

of course later i will refetch the collection to sync with db.

thanks.

sunnyman
User
Posts: 51
Joined: 21-Feb-2007
# Posted on: 01-May-2007 06:03:47   

similar problem from another view,

i fetch a collection from table1 and then i want to 1- delete all records from table1 (using another collection for example) 2- mark entities in my collection as new 3- save the collection

(to implement the last-win on a detail table) this also gives me enclosed exception

thanks

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 01-May-2007 08:25:29   

I want SaveMulti to success even if some entities is deleted by another user form db, in normal cases it will throw ORMConcurrencyException.

In this case, you shouldn't use concurrency control.

Concurrency control makes sure that the entities being saved are found in the database, with the concurrency predicate unchanged.

sunnyman
User
Posts: 51
Joined: 21-Feb-2007
# Posted on: 01-May-2007 13:35:46   

hi, i understand this but i dont set Entity.ConcurrencyPredicateFactoryToUse to anything here is an example to clarify what i want to do

dim col as new PersonCollection col.getMulti(nothing)

now, if another user del the person records from the db, i still want to save my col, so i completely eliminate other user changes

col.savemulti() ' Gives me exception - entity enclosed - and this is right couse i try to save entity that was deleted by another user


First question , can i turn off concurrency so SaveMulti doesnt throw this exception

when entity is deleted and just continue to save other entities

now, i want col.savemulti to save the entities even if it is delete by another user, so i try something like this

dim col2 as new PersonCollection col2.DeleteMulti(Nothing) ''Delete All Records

fore each Person in PersonCollection Person.Fields.State = EntityState.[New] ''Mark Entities as New next

col.SaveMulti() ''Save My Collection, but this still gives me the same exception entity enclosed

Second question, how can i do the previous scenario

thanks.

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 01-May-2007 16:04:42   

In what case you need to save an entitycollection that contains entity that have been deleted? If an user delete an entity, it could be add again if there is another user connected? Why don't you save only the entity that have been modified using the isdirty flag?

sunnyman
User
Posts: 51
Joined: 21-Feb-2007
# Posted on: 01-May-2007 16:37:49   

If an user delete an entity, it could be add again if there is another user connected?

sorry i dont understand what you mean, but here is my senario

in master/detail (for ex, invoice/invoiceDetail) in a multi user application, suppose 2 users read the same invoce/invoiceDetail

First user, add/modify/del records from invoiceDetail and save After that Second user jsut modify one record from InvoiceDetail and save

of couse, i can use concurrency here, but the client request to implement LAST_WRITE_WIN for Details as whole, so there is only one solution to save the second user data and fully truncate first user data

1- delete all InvoiceDetail from db 2- save my collection

The same question with another view

Dim Person as new PersonEntity(9) ''Fetch Person with ID=9

is there any way to flag this entity as new entity so when i call Person.Save it will insert new record instead of update the current one ?

i try Person.Fields.State = EntityState.[New] but it doesnt work

the second part of question,

suppose i do it with your way( saving dirty entities only), using SaveMulti, if other user delete this entity from db, SaveMulti will throw ORMConcurrencyException and faile

is there any way to tell SaveMulti to ignore delete entities , and continue save the other entities? (== Prevent SaveMulti from thorw ORMConcurrencyException)

Thanks.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 02-May-2007 10:22:37   

The trick is to use:

Person.IsNew = true;

This should encounter an Insert operation, fields that will be inserted are those fields which have the IsChanged property set to true.

sunnyman
User
Posts: 51
Joined: 21-Feb-2007
# Posted on: 02-May-2007 17:36:20   

Thanks, it works now but you must add obj.IsDirty=True it seems that setting IsChanged = True by code is not enough to tell llblgen that this field is dirty

Dim col as PersonCollection
Dim obj As PersonEntity

 For Each obj In col

                obj.IsNew = True
                obj.IsDirty = True           'A Must

                For Each f As EntityField In obj.Fields
                        f.IsChanged = True
                Next
Next        

col.SaveMulti() 
pvilanova
User
Posts: 24
Joined: 01-Apr-2007
# Posted on: 28-Aug-2007 04:43:39   

Dim col as PersonCollection
Dim obj As PersonEntity

For Each obj In col

                obj.IsNew = True
                obj.IsDirty = True           'A Must

                For Each f As EntityField In obj.Fields
                        f.IsChanged = True
                Next
Next        

col.SaveMulti() 

That's not working for me. There is no ".Renew()" method in entities?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39589
Joined: 17-Aug-2003
# Posted on: 28-Aug-2007 11:22:42   

What does 'doesn't work' mean? The code should work as expected.

Frans Bouma | Lead developer LLBLGen Pro