c# - Modifying Entity Framework Entities from Multiple Threads -
i have ui thread enables user set properties of entity.
i have worker threads automatically modify properties of entity that, importantly, user not have access to.
would safe have different dbcontexts
per thread , depend on programming avoid modifying same property , attempting savechanges()
on context has same entity modified in different ways?
or there safer way of doing this, i.e. way in possible programmer day change code such same property modified 2 different contexts safely? or latter situation 1 programmer has careful/refactor around?
theoretical part
there three ways resolve concurrency issues in multi-threading environment:
- pessimistic, can done
locks
item being edited - no 1 else can edit item being edited. hard implement approach, , way quite bad performance view - editing threads waiting single writer, , wasting system resources. - optimistic, default way resolve issues. main idea continue operation until have success. there lot of algorithm introduced, encourage read whole wiki-article and, may be, additional references and/or books on theme.
- semi-optimistic, hybrid approach, way being used if need
lock
operations, not all.
practice part
entity framework authors encourage use optimistic way in app. the simple use-case add rowversion
or similar-named property model, , catch dbupdatedexception
during update
.
you can use code-first
solution, this:
[timestamp] public byte[] rowversion { get; set; }
or database-first
solution (add column database editor):
after that, code simple case like:
using (var context = new schooldbentities()) { try { context.entry(student1withuser2).state = entitystate.modified; context.savechanges(); } catch (dbupdateconcurrencyexception ex) { console.writeline("optimistic concurrency exception occured"); } }
as understood, have examine properties of data source, suggest read a great article regarding such use-cases (it mvc-application, i'm sure can manage out main idea).
you can find several articles regarding concurrency in entity framework on msdn:
- optimistic concurrency patterns
- resolving optimistic concurrency exceptions reload (database wins)
- resolving optimistic concurrency exceptions client wins
- custom resolution of optimistic concurrency exceptions
- custom resolution of optimistic concurrency exceptions using objects
- working property values
- getting , setting current or original value of individual property
- getting , setting current value of unmapped property
- checking whether property marked modified
- marking property modified
- reading current, original, , database values properties of entity
- setting current or original values object
- setting current or original values dictionary
- setting current or original values dictionary using property
- creating cloned object containing current, original, or database values
- getting , setting current or original values of complex properties
- using
dbpropertyvalues
access complex properties
as can see, situation relies on developer side, , have great amount of patterns may choose resolve concurrent issues yourself.
Comments
Post a Comment