How to run two Entity Framework Contexts inside TransactionScope without MSDTC?

前端 未结 3 1025
無奈伤痛
無奈伤痛 2021-02-07 09:39

This problem is not readily reproducible in a simple example here but was wondering if anyone has any experience and tips, here is the issue:

  • using Entity
3条回答
  •  隐瞒了意图╮
    2021-02-07 10:24

    I think that what you need to do is to force your contexts to share single database connection. You will be able then to perform these two operations against two different contexts in single transaction. You can achieve this by passing one EntityConnection object to both of your context's constructors. Of course this approach will require you to pass this object to methods which update DB.

    I have recently blogged about creating database context scope which will make using multiple EF contexts and transactions easier.

提交回复
热议问题