using (var ts = new TransactionScope())
{
string connStr = "Data Source=.;Initial Catalog=Test;Integrated Security=true;";
using (SqlConnection connection = new SqlConnection(connStr))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "InsertMessage";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@ID", ));
command.Parameters.Add(new SqlParameter("@Message", "dfd"));
connection.Open();
command.ExecuteNonQuery();
connection.Close();
using (var context = new Entities())
{
context.Message.Add(new Message { ID = , Msg = "dfdfd" });
context.SaveChanges();
}
}
ts.Complete();
}

会报错,除非你打开MSDTS这个windows service
原因是:If you use a single connection within a TransactionScope, it will be done as a local transaction (handled within that connection). If multiple db connections are involved, it will be escalated to a distributed transaction which requires that MSDTC is running on both the machine where your code runs and on the DB server, and that MSDTC on the client has permissions to communicate with MSDTC on the server (http://www.datazx.cn/Forums/en-US/0a365486-c57a-4ee9-a59f-e90a4a4abfbf/action?forum=adodotnetentityframework)

EntityFramework5有一个办法可以解决这个问题

using (var context = new Entities())
{
context.Database.Connection.ConnectionString = connStr;
context.Message.Add(new Message { ID = , Msg = "dfdfd" });
context.SaveChanges();
}

以下版本暂时没找到好的办法,大家有什么方法吗?

Entity Framework,TransactionScope 混合使用的问题讨论的更多相关文章

  1. 重新认识了下Entity Framework

    什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...

  2. 重新认识Entity Framework

    什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...

  3. Entity Framework中DbContext结合TransactionScope提交事务的正确方式

    问: I would like know what is the best possible way to implement transactions with DBContext. In part ...

  4. ORM系列之Entity FrameWork详解

    一. 谈情怀 从第一次接触开发到现在(2018年),大约有六年时间了,最初阶段连接数据库,使用的是[SQL语句+ADO.NET],那时候,什么存储过程.什么事务 统统不理解,生硬的将SQL语句传入SQ ...

  5. Entity Framework 6:专家版本

    随着 Entity Framework 最新主版本 EF6 的推出,Microsoft 对象关系映射 (ORM) 工具达到了新的专业高度,与久负盛名的 .NET ORM 工具相比已不再是门外汉. EF ...

  6. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  7. Entity Framework教程(第二版)

    源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...

  8. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点

    在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...

  9. C# Entity Framework并发处理

    原网站:C# Entity Framework并发处理 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的错误的一种机制.从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NE ...

随机推荐

  1. log4j与commons-logging,slf4j的关系(转)

    前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...

  2. jQuery表格排序总成-tablesorter

    一个.进口单证 <script type="text/javascript" src="js/jquery.js"></script> ...

  3. java多线程(同步和死锁,生产者和消费者问题)

    首先我们来看看同步与死锁: 所谓死锁.这是A有banana,B有apple. A至B说:你把apple对我来说,,我会banana给你. B至A说:你把banana对我来说,,我会apple给你. 可 ...

  4. ftk学习记录(脚本文章)

    [声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上一篇说到了对话框.今天就看看结果. watermark/2/text/aHR0cDovL2Js ...

  5. 2136 Largest prime factor(打表)

    Problem Description Everybody knows any number can be combined by the prime number.Now, your task is ...

  6. DataTable转化为List

    public List<T> ConvertToList<T>(DataTable dt) where T : new()        {            // 定义集 ...

  7. 至尊快速,国产语言RPP 1.83强势来袭

    以下是 R++的性能測试数据:(奔腾 1.86GHZ,測试 3 次取平均值) 执行效率: R++的内部结构和 C++大致同样,所以理论上 R++能够达到和 C++一样的执行速度,眼下 R++已开启汇编 ...

  8. 接收终端Request.InputStream阅读

    接收终端Request.InputStream阅读请求页面参数,最后字符串. byte[] byts = new byte[HttpContext.Current.Request.InputStrea ...

  9. 建立Cent OS7server有些问题需要注意

    1.与网络有关的问题 (1)网络配置问题 CentOS 7 与曾经的版本号项目在网络配置等许多的地方都做了比較大的改动,比如你会发现ifconfig命令没有了... 这是由于,centos7以下使用: ...

  10. UML九种图汇总

    UML视频读,该文件开始起草.我不知道如何下手啊!我想先UML九图和总结的关系,然后开始用它的文件. 首先在地图上. UML的九种图各自是:用例图.类图.对象图.状态图.活动图.协作图.序列图.组件图 ...