我正在使用 EF7 beta3。
在我的数据库上执行两个查询时,如果第一个查询没有返回任何结果并且有一个包含,第二个查询将失败并出现 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
异常(exception)情况是:
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, TaskCompletionSource
1 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.QueryingEnumerable
1.Enumerator.MoveNext() at System.Linq.Enumerable.d__82.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator
2.MoveNext() at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 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版本的问题,但是在当前的开发分支中已经解决了。
关于c# - 使用不产生任何结果的 Include 将关闭阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29147437/