EntityFramework4.0中遇到New transaction is not allowed because there are other threads running in the session
2011-03-22 15:08
736 查看
在使用EntityFramework4.0时,我们遇到这样的Exception:Newtransactionisnotallowedbecausethereareotherthreadsrunninginthesession,是在这样的场景下面:
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
我们使用是Northwind示例Database,你注意要上面代码中有foreach。主要原因是我们的foreach的循环时,我们不能在同一个连接中同时Reader读数据,又同时做Update,它还没有读完。这时你需要把这个集合对象转换成Array或List<T>,将代码修改为这样就可以了:
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
注意返回的数量很大时,类似填充List方法将会消耗很多内存。此时,建议你使用skip方式对结果集进行数据分页处理。关于Linq中的数据分页你可参考这篇POST.
希望对您开发有帮助。
作者:PetterLiu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-PetterLiuBlog。
[Test]
[ExpectedException(typeof(System.Data.EntityException))]
publicvoidTestUnknowIssue2()
{
using(vardb=newTestPerformaceDBEntities())
{
varproducts=db.Products.Where(p=>p.CategoryID==1);
foreach(varproductinproducts)
{
Debug.WriteLine("Getit");
product.ProductName="newname";
//Herewillthrow:Newtransactionisnotallowedbecausethereareotherthreadsrunninginthesession.
db.SaveChanges();
}
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
我们使用是Northwind示例Database,你注意要上面代码中有foreach。主要原因是我们的foreach的循环时,我们不能在同一个连接中同时Reader读数据,又同时做Update,它还没有读完。这时你需要把这个集合对象转换成Array或List<T>,将代码修改为这样就可以了:
[Test]
publicvoidTestUnknowIssue2()
{
using(vardb=newTestPerformaceDBEntities())
{
varproducts=db.Products.Where(p=>p.CategoryID==1).ToList();
foreach(varproductinproducts)
{
Debug.WriteLine("Getit");
product.ProductName="newname";
db.SaveChanges();
}
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
注意返回的数量很大时,类似填充List方法将会消耗很多内存。此时,建议你使用skip方式对结果集进行数据分页处理。关于Linq中的数据分页你可参考这篇
希望对您开发有帮助。
作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-
相关文章推荐
- PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve
- 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-
- The method replace(int, Fragment) in the type FragmentTransaction is not app 解决方法
- ant批量运行Jmeter脚本遇到 Content is not allowed in prolog.问题及解决方案
- - s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:app
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or rem
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into
- Request header field sessionId is not allowed by Access-Control-Allow-Headers in preflight response.
- A new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information)
- 解决配置SQL MDS服务时遇到“The required .svc handler mappings are not installed in IIS”错误
- Detected Maven Version: 3.0.2 is not in the allowed range 3.0.3.
- Unable to use SQL Server because ASP.NET version 2.0 Session State is not installed on the SQL serve
- invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
- WP7“Navigation is not allowed when the task is not in the foreground.”解决方案
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable...
- Logstash could not be started because there is already another instance using the configured data di
- The method replace(int, Fragment) in the type FragmentTransaction is not applica
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into
- CodeIgniter “The filetype you are attempting to upload is not allowed.”解决
- Windows - could-not-be-started-because-the-hypervisor-is-not-running