EF存储库中的交易范围? [英] Transaction scope in EF Repository?

查看:100
本文介绍了EF存储库中的交易范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用存储库模式与EF。在我的项目中,我们使用两个数据库,这两个数据库位于两个不同的项目中。任何时候,一个项目是CoreLib(我们在另一个)。我有以下问题。


  1. 我可以为这两个项目使用一个存储库层吗?

  2. 如何使用System.Transactions.TransactionScope提供交易安全?
    注意:我正在使用Microsoft的统一框架和UnitOfWork模式。






感谢您的回复。我已经实现了在事务范围内保存上下文更改的功能。每次都会抛出以下异常。

  {分布式事务管理器(MSDTC)的网络访问已被禁用,请使用组件服务管理工具在MSDTC的安全配置中启用DTC进行网络访问。} 

I认为这与MSDTC配置有关,我在客户端和SQL服务器机器中配置了网络DTC访问。配置如下。

 网络DTS访问 - 启用。 
允许远程客户端 - 启用。
允许远程管理 - 启用。
允许入站 - 启用。
允许outbond - 启用。
无需验证 - 启用。

在配置MSDTC时是否有任何错误?


$ b $另一个问题:这个配置是否与域配置相关联?
因为在我们的环境中,我的数据库服务器没有用它的名称解析(我们正在使用IP地址)。

解决方案


我可以为这两个项目使用一个存储库层。


是的。


如何使用System.Transactions.TransactionScope提供事务安全?注意:我正在使用Microsoft的统一框架和UnitOfWork模式。


在业务层面使用事务划分(业务需要)所有的东西,必须在一个事务中的一个事务c)。



由于您正在使用UnitOfWork模式。


  1. 一个工作单位与事务无关(在数据库意义上)。工作单位决定了 ObjectContext 的生命周期。您可以随时创建TransactionScope。在创建工作单元之后或之前创建它不重要。

  2. 由于UnityFW是ioc容器,因此可以在业务方法上使用属性进行事务划分。

现在有些事情涉及交易和EF:


  1. 事务与 DbConnections 配合使用,与EF无关。

  2. 如果所有涉及的驱动程序都支持两阶段提交,那么您可以将一些旧的SQL东西与EF东西,甚至一个数据库混合在一个事务中。

  3. ObjectContext 的SaveChanges 将参与全局事务。如果没有全球交易,它将创建一个新的事务,保存所有的东西并提交或回滚。


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).

解决方案

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.

这篇关于EF存储库中的交易范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆