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 ...
随机推荐
- oracle PL/SQL(procedure language/SQL)程序设计之函数+过程+包(转)
匿名PL/SQL块回顾 DECLARE (可选) 定义在PL/SQL块中要使用的对象 BEGIN (必须) 执行语句 EXCEPTION (可选) 错误处理语句 END; (必 ...
- SqlServer发送邮件,定时作业
今天偶然研究了一下sqlserver发送邮件的功能,之前听说过可以发,但是一直没尝试过,只是用C#写后台程序的方式来发邮件. 现在又多了一种发送邮件的途径. 大致的步骤如下: 1.配置sqlserve ...
- 下载文档--Struts2中国的文件下载被显示为空间的问题
下载文档--Struts2中国人似乎是空文件下载格问题 前言:近期公司项目中用到文件下载,依据底层,决定使用struts2的文件下载模式. 乱码大多数都攻克了,竟然出现中文文件下载时,中文文字显示为空 ...
- HDU 2841 Visible Trees(数论)
标题效果:给你个m*n方格,广场格从(1,1)开始. 在树中的每个点,然后让你(0,0)点往下看,问:你能看到几棵树. 解题思路:假设你的视线被后面的树和挡住的话以后在这条线上的树你是都看不见的啊.挡 ...
- hdu 1712 (分组背包入门)
http://acm.hdu.edu.cn/showproblem.php?pid=1712 问题 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].这些物品被划分为若干组, ...
- c#数据库操作大全
原文:c#数据库操作大全 1.提取单条记录 //using System.Data; //using System.Data.SqlClient; using (SqlConnection cn = ...
- unity3d插件Daikon Forge GUI 中文教程-1-Daikon Forge介绍
(游戏蛮牛首发)大家好我是孙广东官网提供了专业的视频教程http://www.daikonforge.com/dfgui/tutorials/,只是是在youtube上,要观看是须要FQ的. 只是教程 ...
- 模板专业化和模板偏特样片(template specialization and partial template specialization)
测试环境: win7 64 g++ 4.8.1 /*************************************************************************** ...
- struts2注解redirect传递参数解决方案时,中国的垃圾问题
struts2注解redirect传递参数解决方案时,中国的垃圾问题 试过很多方法 tomcat 编码 .字符串转换 .URLEncoder .. 但是,没有解决方案,然后仔细阅读 stru ...
- bootstrap基本标签总结2
[布局]bootstrap基本标签总结2 缩略图 <div class="container"> <div class="row"> ...