Is it possible to use one transaction for two different contexts?
I have a dll that has an entity model that does a particular job. My main application uses that dll but also has it's own entity model to talk to the DB.
When I save an entity in the main application that triggers a context save of the dll entities, could I wrap them both in a single tran开发者_如何学编程saction?
You can, using TransactionScope
.
You should be aware, though, that the transaction will be handled by MSDTC rather than at the database level alone.
A better option would be to use the Unit of Work and Repository patterns to ensure that each operation in the chain of events (a single unit of work) used the same context. At the end of the set of operations, you would call SaveChanges a single time which would use a DB transaction.
The two contexts are different. I have an entity model called Location.edmx in a dll that knows how to store a location. In the main application I have and entity model called Artifacts.edmx that knows how to store an artifact.
An artifact has a location. In the artifact entity code I would write:
public void Save()
{
context1.saveChanges();
location.Save()
}
in location.Save()
it would be
public void Save()
{
context2.saveChanges();
}
Note that these two methods are in different dlls.
How would I use transactionScope in my example?
精彩评论