用于WinXP的实体框架5和用于Win7精确代码的实体框架6,用于WinXP的EntityCommandExcecutionException [英] Entity Framework 5 for WinXP and Entity Framework 6 for Win7 exact Code, EntityCommandExcecutionException for WinXP

查看:102
本文介绍了用于WinXP的实体框架5和用于Win7精确代码的实体框架6,用于WinXP的EntityCommandExcecutionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我得到一个我无法处理的奇怪异常。



关注:



我构建了一个应用程序,它从MySQL数据库中获取文件并将其写入项目文件夹。

我先写了一篇文章Win7版本使用Entity Framework 6并由Visual Studio 2012编译。

Visual Studio 2012不支持WinXP应用程序,因此我不得不使用Entity Framework 5重建我的应用程序并使用Visual编译它Studio 2010.



我的应用程序代码在两个版本中完全相同。



我试试使用以下代码将存储在mysql数据库中的文件写入本地项目文件夹:



  //  获取Projectname&的idProject。 Projectversion  
var idProject =( from x in ebi.project
其中​​ x.Projectname.Equals(project_name)&& x.Projectversion.Equals(project_version)
选择 x.idProject);


foreach var id in idProject)
{
// 获取所有文件属于idProject&编解码器
var file =( from x in ebi.documents
其中​​ x.idProject.Equals(id)&& x.Folder.Equals(< span class =code-string> 编解码器
选择 X);

foreach var itm 文件中)
{
尝试
{
// 获取并编写所有文档。
System.IO.File.WriteAllBytes(foldername + itm.Document_Name,itm.File.ToArray());

}
catch (InvalidOperationException IOE)
{
// 显示错误消息。
MessageBox.Show( < span class =code-string>无法创建连接!\\\\ n +
IOE.Message);
// 退出事件处理程序。
返回;
..................
..................
}





适用于Win7和Entity Framework 6,但不适用于带有实体框架5的WinXP版本。

调用异常:EntityCommandExcecutionException并由以下行引起:



  //  获取属于idProject& amp;的所有文件。编解码器 
var file =( from x in ebi.documents
其中​​ x.idProject.Equals(id)&& x.Folder.Equals(< span class =code-string> 编解码器
选择 x);







我不知道如何处理。请帮忙!



问候



Adam

解决方案

嗨!在我得到正确的内部异常之后我找到了解决方案:已经有一个与此Connection相关联的开放DataReader必须先关闭



我必须添加。每个查询后的toList()。 - >这会强制实体框架将列表加载到内存中,因此在一次查询后关闭datareader。然后执行下一个查询。



正确代码:





  //  获取Projectname&的idProject Projectversion  
var idProject =( from x in ebi.project
其中​​ x.Projectname.Equals(project_name)&& x.Projectversion.Equals(project_version)
选择 x.idProject).ToList();


foreach var id in idProject)
{
// 获取所有文件属于idProject&编解码器
var file =( from x in ebi.documents
其中​​ x.idProject.Equals(id)&& x.Folder.Equals(< span class =code-string> 编解码器
选择 x)的.ToList();
...............
;


Hi guys!

I get a strange Exception that I could not handle.

Following:

I build an application which gets files from a MySQL-Database and write it to the project-folder.
I wirst wrote an Win7 Version which uses the Entity Framework 6 and was compiled by Visual Studio 2012.
Visual Studio 2012 does not support WinXP Applications, so I had to rebuild my application with the Entity Framework 5 and compiled it using Visual Studio 2010.

The Code of my application is exactly the same in both Versions.

I try to write files which are stored in a mysql-database to the local project-folder using this piece of code:

//get the idProject for the Projectname & Projectversion
var idProject = (from x in ebi.project
                 where x.Projectname.Equals(project_name) && x.Projectversion.Equals(project_version)
select x.idProject);


foreach (var id in idProject)
{
//get all files that belong to the idProject & the codec
var file =(from x in ebi.documents
where x.idProject.Equals(id) && x.Folder.Equals("Codec")
select x);

foreach (var itm in file)
{
try
{
//Get and write all documents. 
System.IO.File.WriteAllBytes(foldername + itm.Document_Name, itm.File.ToArray());

}
catch (InvalidOperationException IOE)
{
// Show an error message.
MessageBox.Show("Cannot Create the Connection!\r\n" +
IOE.Message);
// Exit the event handler.
return;
..................
..................
}



It works on Win7 and Entity Framework 6 but not on the WinXP Version with Entity Framework 5.
The Exception is called: EntityCommandExcecutionException and caused by the line:

//get all files that belong to the idProject &amp; the codec
                      var file =(from x in ebi.documents
where x.idProject.Equals(id) && x.Folder.Equals("Codec")
                                 select x);




I don´t know how to handle that. Please help!

regards

Adam

解决方案

Hi! I found the solution after I got the correct inner Exception: "There is already an open DataReader associated with this Connection which must be closed first"

I had to add .toList() after each query. --> This forces entityframework to load the list into memory, and so after one query the datareader is closed. Then the next query is performed.

Correct Code:


//get the idProject for the Projectname & Projectversion
var idProject = (from x in ebi.project
                 where x.Projectname.Equals(project_name) && x.Projectversion.Equals(project_version)
select x.idProject).ToList();


foreach (var id in idProject)
{
//get all files that belong to the idProject & the codec
var file =(from x in ebi.documents
where x.idProject.Equals(id) && x.Folder.Equals("Codec")
select x).ToList();
...............
;


这篇关于用于WinXP的实体框架5和用于Win7精确代码的实体框架6,用于WinXP的EntityCommandExcecutionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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