Click here to Skip to main content
11,429,154 members (61,243 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# CodeFirst
I need to perform the following, in EF CodeFirst

Retrieve a record, Delete it.
Do other tasks.
If other tasks are done successfully, the deletion needs to be commited.
Otherwise, the deletion needs to be rolled back as if the record has never been deleted before.

Does anyone has a sample C# codefirst to perform this commitment control.
I search the web and can not find any sample of it.

Thanks beforehand.
Posted 23-Feb-13 3:47am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There's nothing special about this. When you Remove the record from EF it's just TAGGED to be deleted. It won't be deleted from the database until you call SaveChanges on your DbContext.

So, if you don't want the record to be removed, just change its state back to Unchanged:
  myDbContext.Entry(myEntityObject).State = EntityState.Unchanged

But, you've got a bigger problem in your logic. Why are you even "deleting" the record unless you have confirmed that it needs to be deleted?
  Permalink  
Comments
astuserp at 24-Feb-13 21:10pm
   
Thanks Dave for your idea.

Sorry for not giving a proper reason why I need to delete the record in the first place.
The reason that I retrieve a record and delete it immediately is because I don't want the
same record being retrieved by others.
So once I retrieve a record, the same record can not be retrieved by other users.

I know that you may say that put a flag in the record that signify it is being used. But the problem with this kind of flag is when suddenly there is a power outage and the server is down. Then when the server is up again, the record is still being flaged.

But thanks again for your idea to delay SaveChange. It was not thought off at all.
Dave Kreskowiak at 24-Feb-13 21:17pm
   
Uhhh....Because SaveChanges hasn't been called, other users can still find the recond in the database.

You have a large problem. If you delete the record, other users won't be able to find it, but if you then put the record back, and your tables are setup to autogenerate record IDs and/or cascade deleted is on, your record will now have a different ID in the database, completely screwing up your relationships.

I think you have to rethink what you're doing and why.
astuserp at 24-Feb-13 22:02pm
   
Hi Dave, Thanks for quick response.

The file is actually a standalone file. It has no relationship with other file. Also the autogenerate record ID is not really in use. Actually, I can retrieve the record using traditional ADO.NET but since other files are created with CodeFirst, then I think why not use the same method to retrieve the record.
If I can not find any elegant way to perform the traditional record processing in CodeFirst, then I think I will use the old way of retrieving the record.

Thanks Dave.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100