为了向自己介绍实体框架,我创建了一个与之配合使用的控制台应用程序。
有两个实体: ClassA 和 ClassB 。他们之间有一对多的关系。 ClassA的一个实例可以有多个ClassB实例。 ClassB的一个实例具有0或1个ClassA实例。
类的代码是:
public class ClassA {
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual ICollection<ClassB> ClassBs {get; set;}
}
public class ClassB {
public virtual int Id {get; set;}
public virtual string Name {get; set;}
public virtual ClassA ClassA {get; set;}
}
我的数据库上下文和初始化程序的代码是:
public class Context : DbContext {
public DbSet<ClassA> ClassAs {get; set;}
public DbSet<ClassB> ClassBs {get; set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<ClassA>()
.HasMany(classA => classA.ClassBs)
.WithRequired(classB => classB.ClassA);
base.OnModelCreating(modelBuilder);
}
}
public class Initializer : DropCreateDatabaseAlways<Context> {
protected override void Seed(Context context) {
base.Seed(context);
for (int i = 1; i <= 3; i++) {
var classA = new ClassA{
Name = "A-" + i,
ClassBs = new LinkedList<ClassB>()
};
for (int j = 1; j <= 3; j++) {
var classB = new ClassB{
Name = "B-" + i + "-" + j,
ClassA = classA
};
classA.ClassBs.Add(classB);
context.ClassBs.Add(classB);
}
context.ClassAs.Add(classA);
}
context.SaveChanges();
}
}
我的主要方法是:
static void Main(string[] args) {
Database.SetInitializer(new Initializer());
Context db = new Context();
foreach (var classA in db.ClassAs) {
Console.WriteLine(classA.Name);
foreach (var classB in classA.ClassBs)
Console.WriteLine("\t" + classB.Name);
}
Console.Write("\nFIN");
Console.ReadKey();
}
当Main方法中的第二个foreach循环启动时,抛出以下异常:
System.Data.EntityCommandExecutionException was unhandled
Message=An error occurred while executing the command definition. See the inner exception for details.
Source=System.Data.Entity
InnerException: System.InvalidOperationException
Message=There is already an open DataReader associated with this Command which must be closed first.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command)
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
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(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
似乎ClassBs
中的ClassA
属性未正确加载。但是我确实在上下文中指出ClassA
有很多ClassB
。
数据库连接似乎很好,因为ClassA的第一个实例显示在控制台上。我也可以在Visual Studio中访问数据库,并查看初始化程序中生成的数据。
我使用training videos of ASP.NET MVC开始使用此控制台应用程序。
你知道出了什么问题吗?
答案 0 :(得分:4)
您需要将连接字符串中的multipleactiveresultsets设置为true。
MultipleActiveResultSets = TRUE;
答案 1 :(得分:0)
问题可能与模型的构建方式无关 - 更可能与您是否在连接上启用MultipleActiveResultSets
有关。见Entity Framework: There is already an open DataReader associated with this Command