使用一个连续运行多次的联合查询,我得到每个字母字母的UserlistSection
对象,如果Users
以起始字母存在。
所以我得到了这样的东西:
用户列表部分“A”
用户
用户
用户列表部分“H”
用户
用户列表部分“Y”
用户
用户
用户
但是查询抛出以下异常
MySql.Data.MySqlClient.MySqlException: There is already an open DataReader associated with this Connection which must be closed first.
查询任务
public UserRepository(myDatabase database)
{
_database = database;
}
public Task<ObservableCollection<User>> GetUserFriendsAsync(int id, string letter)
{
return Task.Factory.StartNew(() =>
{
try
{
var query1 = (from urs in _database.user_relationships
join u in _database.users on urs.relationship_user_from equals u.user_id
where urs.relationship_user_to == id
&& u.user_name.StartsWith(letter)
&& urs.relationship_status == 1
select new User
{
Id = u.user_id,
Username = u.user_name,
AvatarByte = u.user_avatar,
Gender = u.user_gender,
GroupId = u.user_usergroup,
LoginStatusId = u.user_loginstatus
});
var query2 = (from urs in _database.user_relationships
join u in _database.users on urs.relationship_user_to equals u.user_id
where urs.relationship_user_from == id
&& u.user_name.StartsWith(letter)
&& urs.relationship_status == 1
select new User
{
Id = u.user_id,
Username = u.user_name,
AvatarByte = u.user_avatar,
Gender = u.user_gender,
GroupId = u.user_usergroup,
LoginStatusId = u.user_loginstatus
});
// Union to get users via relationships (from - to and to - from)
var queryUnion = query1.Union(query2).OrderBy(u => u.Username);
return new ObservableCollection<User>(queryUnion);
}
catch (Exception ex)
{
Trace.WriteLine("UserRepository - GetUserFriendsAsync" + ex);
return null;
}
});
}
以及创建集合的对象/方法
public class UserlistSection : ObservableObject
{
#region Attributes
private string _letter;
private ObservableCollection<User> _users;
#endregion Attributes
public string Letter
{
get { return _letter; }
set { Set(ref _letter, value); }
}
public ObservableCollection<User> Users
{
get { return _users; }
set { Set(ref _users, value); }
}
}
// -------------------------------------------------------------------------
public async void UpdateFriendlistCollection()
{
IsLoading = true;
Friendlist = new ObservableCollection<UserlistSection>();
for (var c = 'A'; c <= 'Z'; c++)
{
try
{
var letter = c.ToString();
var userCollection = await _userRepository.GetUserFriendsAsync(_userObj.Id, letter);
if (userCollection.Count < 1) return;
foreach (var friend in userCollection)
{
friend.AvatarImage = friend.GetAvatarImgByPixel(200);
}
var section = new UserlistSection
{
Letter = letter,
Users = userCollection
};
Friendlist.Add(section);
}
catch (Exception ex)
{
Trace.WriteLine("FriendlistViewModel - UpdateFriendlistCollection: " + ex.Message);
}
}
IsLoading = false;
}
可能有更好的方法来构建查询,但我还不知道如何构建。所以,如果有人能看到我的错误,以避免懒散地加载/解决异常,那就太好了。
如果我遗漏了一些解释,请告诉我。
编辑
当我像那样添加
MultipleActiveResultSets=True
时,它告诉我关键字不支持?<connectionStrings>
<add name="someDatabase" connectionString="metadata=res://*/Repository.Database.SomeDatabase.csdl|res://*/Repository.Database.SomeDatabase.ssdl|res://*/Repository.Database.SomeDatabase.msl;provider=MySql.Data.MySqlClient;provider connection string="server=127.0.0.1;user id=someUser;persistsecurityinfo=True;password=somePassword;multipleactiveresultsets=True;database=someDatabase;"" providerName="System.Data.EntityClient" />
System.ArgumentException: Keyword not supported.
Parametername: multipleactiveresultsets
bei MySql.Data.MySqlClient.MySqlConnectionStringBuilder.GetOption(String key)
bei MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(String keyword, Object value)
最佳答案
您应该在config的connectionstring中启用MARS。
将"MultipleActiveResultSets=True"
添加到连接字符串。
更多info。
编辑
从MSDN:
在For Each(C#)期间调用Load方法时
(Visual Basic)枚举,实体框架尝试打开一个新的
数据读取器。此操作将失败,除非您启用了多个
通过在
连接字符串。有关详细信息,请参见使用多个活动
MSDN上的结果集(MARS)。您还可以加载查询的结果
进入列表集合,它关闭数据读取器并使您
枚举集合以加载引用的实体
编辑
对于MySQL,我找到了this。也许这对你有帮助。
似乎MySQL不支持MARS。如果可以更改MS SQL Server Express的数据库。或者更改ORM,例如NHibernate。
关于c# - Entity Framework 避免LazyLoading/已经打开的DataReader问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35623705/