Transaction scope in EF Repository?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 10:49:25

问题


I am using the repository pattern with EF. In my project we are using two databases and these two databases are sitting in two different projects. For any time one project is a CoreLib (we are referring in the other). I have the following questions.

  1. Can I use one repository layer for the two projects?
  2. How can I provide the transaction safety using System.Transactions.TransactionScope? Note: I am using Microsoft's unity framework and UnitOfWork pattern.

Thanks for your reply. I have implemented the functionality to save the context changes within Transaction scope. Every time, it is throwing the following exception.

{"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."}

I think this is related to MSDTC configuration and I configured network DTC access in client and SQL server machine. The configuration is as follows.

Network DTS access  - Enabled.
Allow Remote Clients    - Enabled.
Allow Remote Administration – Enabled.
Allow Inbound   - Enabled.
Allow outbond   - Enabled.
No Authentication Required – Enabled.

Is there anything I am missing in configuring MSDTC?

One more question: Is this configuration linked to Domain configuration? Because in our environment my DB server is not resolved with its name (we are using the IP address).


回答1:


Can I use one repository layer for the two projects?

Yes.

How can I provide the transaction safety using System.Transactions.TransactionScope? Note: I am using the Microsoft’s unity framework and UnitOfWork pattern.

Use transaction demarcation in the business layer as the business requires it (do all stuff that must be in one transaction in a transactionscope).

Since you are using the UnitOfWork pattern.

  1. A unit of work has nothing to do with a transaction (in the db sense). The unit of work determines the lifetime of the ObjectContext. You can create the TransactionScope as you please. It does not matter if you create it after or before you create the unit of work.
  2. It is possible to do transaction demarcation with attributes on the business methods since the UnityFW is an ioc container.

Some stuff to now about transactions and EF:

  1. Transactions work with DbConnections and have not much to do with EF.
  2. You can mix old SQL stuff with EF stuff and even more than one database in one transaction if all involved drivers support the 2 phase commit.
  3. SaveChanges of ObjectContext will participate in a global transaction. If there is no global transaction, it will create a new transaction, save all stuff and commit or rollback.



回答2:


you can do something like this. Assuming your Two database contexts are named FirstContext andSecondContext`

public class ContextFacade : IUnitOfWork // your Unit of work interface
{
  FirstContext _fc;
  SecondContext _sc
  public ContextFacade(FirstContext fc, SecondContext sc)
  {
     _fc = fc;
     _sc = sc;
  }

  public void SaveChanges()
  {
    var scope = new TransactionScope(TransactionScopeOption.Required, options);
    using(scope)
    {
      _fc.SaveChanges();
      _sc.SaveChanges()

      scope.Complete();
    }
  }
}

Take a look at Ladislav Mrnka's answer on how all of these can be put together.



来源:https://stackoverflow.com/questions/6401009/transaction-scope-in-ef-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!