Entity Framework - read write to write

I use the same database from various applications using the Entity Framework. However, when one application reads / updates a record, I do not want other applications to read this data.

I tried using the following code example; however, they can still read the recording.

Any suggestion OR different approaches would be highly appreciated!

using (Entities context = new Entities(ConnectionString)) { DBTable entity; do { entity = context.DBTable .Where(item => item.IsLock == false) .FirstOrDefault(); if (entity != null) { // Map entity to class ...... // Lock the record; it will be unlocked later by a calling method. entity.IsLock = true; context.SaveChanges(); count++; } } while (entity != null && count < 100); } 

Edited: Basically, I read a record and do something (sometimes it takes a lot of time). Then update the record using the success / failure flag (if you cannot perform another action again). I do not want other applications to complete the task successfully several times.

+4
source share
1 answer

Moved from comment to answer:

If you don't mind fixing it with SQL, create a stored procedure. In the stored procedure, make SELECT in the WITH UPDLOCK entry ( see the table tooltip information here ). After SELECT, update the record to IsLock. This prevents any other process from reading while checking / setting the IsLock field.

Hope that helps

+5
source

All Articles