爲了向自我介紹實體框架,我創建了一個可與之協同工作的控制檯應用程序。實體框架中的一對多關係導致異常
有兩個實體:A類和B類。它們之間有一對多的關係。一個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)
看起來ClassA
的ClassBs
屬性沒有正確加載。但是我在上下文中指定了ClassA
有很多ClassB
。
數據庫連接看起來很好,因爲ClassA的第一個實例顯示在控制檯上。我也可以訪問Visual Studio中的數據庫並查看初始化程序中生成的數據。
我使用training videos of ASP.NET MVC開始使用此控制檯應用程序。
你知道發生了什麼問題嗎?
嘗試使用db.Configuration.LazyLoadingEnabled = true創建db上下文後將延遲加載設置爲true; – 2012-03-02 13:48:08