使用不产生任何结果的包含将关闭 reader

Using Include that produces no result will close the reader

我正在使用 EF7 beta3。

在我的数据库上执行两个查询时,如果第一个查询 returns 没有结果并且有一个包含,第二个查询将失败 System.InvalidOperationException。我无法在 MusicStore 中重现该问题,也无法找出我的代码中的原因。

抛出异常:

var db = new EmfdsContext();
var user = db.Users.Include(x => x.Logins).FirstOrDefault(x => x.UserName == "nonExistantUserName");
var role = db.Roles.Where(x => x.Name == "Subscriber").ToList(); //throws

不抛出异常:

var db = new EmfdsContext();
var user = db.Users.FirstOrDefault(x => x.UserName == "nonExistantUserName");
var role = db.Roles.Where(x => x.Name == "Subscriber").ToList(); //doesn't throw

不抛出异常:

var db = new EmfdsContext();
var user = db.Users.Include(x => x.Logins).FirstOrDefault(x => x.UserName == "existantUserName");
var role = db.Roles.Where(x => x.Name == "Subscriber").ToList(); //doesn't throw

例外情况是:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.Core.dll but was not handled in user code

Additional information: ExecuteReader requires an open and available Connection. The connection's current state is closed.

登录包含无关紧要,它可以是包含中的任何其他关系,它会抛出异常。

相关映射:

builder.Entity<Subscriber>().ForRelational().Table("Subscribers", "ApplicationServices");
builder.Entity<Subscriber>().Key(s => s.Id);
builder.Entity<Subscriber>().HasMany(s => s.SubscriberGroups).WithOne(sg => sg.Subscriber).ForeignKey(sg => sg.Subscriber_Id);
builder.Entity<Subscriber>().HasMany(s => s.Roles).WithOne().ForeignKey(ur => ur.UserId);

builder.Entity<Role>().ForRelational().Table("Roles", "ApplicationServices");
builder.Entity<Role>().Key(r => r.Id);
builder.Entity<Role>().HasMany(r => r.Users).WithOne().ForeignKey(ur => ur.RoleId);
builder.Entity<Role>().HasMany(r => r.MenuRoles).WithOne(mr => mr.Role).ForeignKey(mr => mr.Role_Id);

堆栈跟踪:

at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.Data.Entity.Relational.Query.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.d__82.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext() at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at EMFDS.WebApplication.Controllers.HomeController.Index() in C:\Users\nicolas.bourgoin\Documents\Visual Studio 2015\Projects\emfds_dev2\emfds\src\EMFDS.WebApplication\Controllers\HomeController.cs:line 25

更新 1

终于,我可以用 MusicStore 重现这个问题了。但是,它只会在您包含一对多关系(集合)时抛出异常。如果包含多对一关系,则不会抛出异常。

我回过头来验证这是EF7的beta3版本的问题,但是在当前的开发分支中已经解决了。