I am accessing my database via Entity Framework 4.
I have a server that listens on a port, awaiting some messages. When a message comes, it is translated into a table row and it should be inserted into the database. However, multiple messages can come at once. For each message I create a Task (from TPL) and execute it asynchronously.
Each of these tasks creates an instance of the ObjectContext, creates an object of the appropriate entity class (represents a table in DB), inserts the data into the ObjectContext, and then calls the SaveChanges method.
Thus each thread created its own ObjectContext.
Can an instance of the ObjectContext influence any other instance of the ObjectContext?
Would this scenario have any side-effects?
(Note that the data inserted won’t create any referential integrity error).
In your situation transactional integrity is guaranteed by the database (SQL Server) and not Entity Framework. Since each thread has its own context you don’t have to worry about each context’s
SaveChanges()interfering with another.One instance of
ObjectContextcan not influence another one. You may run into a situation where oneSaveChanges()modifies the database in a way that causes a subsequentSaveChanges()on a different context to fail. But this is by design and, again, is a result of SQL Server enforcing its constraints.