Entity Framework,TransactionScope 混合使用的问题讨论
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 混合使用的问题讨论的更多相关文章
- 重新认识了下Entity Framework
什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...
- 重新认识Entity Framework
什么是Entity Framework Entity Framework是一个对象关系映射O/RM框架. Entity Framework让开发者可以像操作领域对象(domain-specific o ...
- Entity Framework中DbContext结合TransactionScope提交事务的正确方式
问: I would like know what is the best possible way to implement transactions with DBContext. In part ...
- ORM系列之Entity FrameWork详解
一. 谈情怀 从第一次接触开发到现在(2018年),大约有六年时间了,最初阶段连接数据库,使用的是[SQL语句+ADO.NET],那时候,什么存储过程.什么事务 统统不理解,生硬的将SQL语句传入SQ ...
- Entity Framework 6:专家版本
随着 Entity Framework 最新主版本 EF6 的推出,Microsoft 对象关系映射 (ORM) 工具达到了新的专业高度,与久负盛名的 .NET ORM 工具相比已不再是门外汉. EF ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- Entity Framework教程(第二版)
源起 很多年前刚毕业那阵写过一篇关于Entity Framework的文章,没发首页却得到100+的推荐.可能是当时Entity Framework刚刚发布介绍EF的文章比较少.一晃这么多年过去了,E ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第一章:创建基本的MVC Web站点
在这一章中,我们将学习如何使用基架快速搭建和运行一个简单的Microsoft ASP.NET MVC Web站点.在我们马上投入学习和编码之前,我们首先了解一些有关ASP.NET MVC和Entity ...
- C# Entity Framework并发处理
原网站:C# Entity Framework并发处理 在软件开发过程中,并发控制是确保及时纠正由并发操作导致的错误的一种机制.从 ADO.NET 到 LINQ to SQL 再到如今的 ADO.NE ...
随机推荐
- ArcGIS 10 破解安装(win7 64位)
上一周换了个win7 64位系统,昨天依照各种各样破解方法装ArcGIS10装了一天,装上之后ArcMap能用,可是装的AO sdk不能用,在VS2008里一拖就报错,确实把我给气坏了.今天早上上班, ...
- acdream 1431 Sum vs Product
Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...
- Parse 和 Swift 搭建一个像 Instagram
如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用? [编者按]本篇文章作者是Reinder de Vries,既是一名企业家,也是优秀的程序员,发表多篇应用程序的博客 ...
- TortoiseGit安装与配置(转)
TortoiseGit 简称 tgit, 中文名海龟Git. 海龟Git只支持神器 Windows 系统, 有一个前辈海龟SVN, TortoiseSVN和TortoiseGit都是非常优秀的开源的版 ...
- 第19章 解释器模式(Interpreter Pattern)
原文 第19章 解释器模式(Interpreter Pattern) 解释器模式 导读:解释器模式,平常用的比较的少,所以在写这个模式之前在博客园搜索了一番,看完之后那叫一个头大.篇幅很长,我鼓足了劲 ...
- Visual Studio GitHub For Windows部署
使用GitHub For Windows部署Visual Studio项目 因为最近同时再看很多技术方面的书,书上的例子有很多自己想亲自尝试一下,但是每次写例子都得创建一个新项目未免太麻烦,索性就整理 ...
- Ubuntu中编译链接Opencv应用的简便方式
安装完毕Opencv后,使用下面命令查 看编译/连接參数 pkg-config --cflags --libs opencv 可看到例如以下信息 -I/usr/include/opencv /usr ...
- 触摸屏touchstart 与 click
设计效果:当手指点击或触摸红框线menuList之外的部分时,弹框menuList消失. 问题:在优化触屏版的时候发现如图问题.当menuList弹出.手指触摸屏幕向下滑动时,menuList弹框不消 ...
- php_中替换换行符
//php 有三种方法来解决 //1.使用str_replace 来替换换行 $str = str_replace(array("\r\n", "\r", &q ...
- C++学习笔记32 断言函数
首先,让我们来看看百度百科上"断言函数"定义说明: 1定义fr=aladdin#" class="nslog:1019" title="编辑 ...