I am using C# and the Entity Framework to access a MySQL database. I am grabbing the results of a stored procedure, and trying to turn them into a list of objects, However whenever it comes to the part what references a table through a one to many relationship, it fails with the error
我正在使用C#和Entity Framework来访问MySQL数据库。我抓住存储过程的结果,并尝试将它们转换为对象列表。但是,无论何时通过一对多关系引用表的部分,它都会因错误而失败
There is already an open DataReader associated with this Connection which must be closed first.
The code I am using is here:
我正在使用的代码在这里:
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarHireCommon.DataObjects;
namespace CarHireServer
{
public class DatabaseHandler
{
protected static DatabaseHandler instance;
protected carhireEntities1 Entities;
public static DatabaseHandler GetInstance()
{
if (instance == null)
instance = new DatabaseHandler();
return instance;
}
public DatabaseHandler()
{
Entities = new carhireEntities1();
}
public List<AvailableAssets> GetAvailableAssets(DateTime startDate, DateTime endDate)
{
ObjectResult<asset> res = Entities.GetAvailableAssets(startDate, endDate);
List<AvailableAssets> list = new List<AvailableAssets>();
foreach(var assetRes in res)
{
AvailableAssets asset=new AvailableAssets();
asset.id = assetRes.id;
asset.Comment = assetRes.comments;
asset.Make = assetRes.make;
asset.Model = assetRes.model;
asset.Fuel = assetRes.fuel;
asset.LongTerm = assetRes.longterm;
// This is the line that errors:
asset.Category = assetRes.category.categoryname;
list.Add(asset);
}
return list;
}
}
}
I have allready told it which table the Stored Procedure returns, and the other variables access correctly.
我已经告诉它存储过程返回哪个表,其他变量正确访问。
I have also tried doing it the long way with:
我也尝试过很长的路要走:
var cat = from b in Entities.categories where b.id == assetRes.category_id select b;
asset.Category = cat.FirstOrDefault<category>().categoryname;
However the thing still exceptions with the exact same error.
然而事情仍然是例外与完全相同的错误。
1 个解决方案
#1
1
I found C# Entity Framework: There is already an open DataReader associated with this Connection which must be closed first which will probably help you exactly with this question.
我找到了C#Entity Framework:已经有一个与此Connection相关联的开放DataReader,必须首先关闭它,这可能会帮助您完全解决这个问题。
GL!
#1
1
I found C# Entity Framework: There is already an open DataReader associated with this Connection which must be closed first which will probably help you exactly with this question.
我找到了C#Entity Framework:已经有一个与此Connection相关联的开放DataReader,必须首先关闭它,这可能会帮助您完全解决这个问题。
GL!