static async Task InsertBug01(ApplicationDbContext dbContext) { //https://github.com/aspnet/EntityFrameworkCore/issues/18263#issuecomment-541369431 var key = new CdKey { //Id = Guid.NewGuid().ToString(), //this bug fix solution Name = "asp.net", BuildType = BuildType.Test }; await dbContext.AddAsync(key); }
static async Task FixChange02(ApplicationDbContext dbContext) { /* * I don't think this lazy loading is bad, instead I think it's very cool. * I hope the official can have some documentation about this, or help other through my example. */ var firstkey = new CdKey { Id = Guid.NewGuid().ToString(), //this bug fix solution Name = "asp.net", BuildType = BuildType.Test }; await dbContext.CdKeys.AddAsync(firstkey); await dbContext.SaveChangesAsync(); var keys = dbContext.CdKeys?.Where(k => k.BuildType == BuildType.Test)?.ToList();//Because the loading is done foreach (var item in keys) { var values = dbContext.keyValues.Where(v => v.KeyId == item.Id).ToList();//So,No error } }
static async Task Change02(ApplicationDbContext dbContext) { //https://github.com/aspnet/EntityFrameworkCore/issues/18358 //fix this, using SqlServer then enable MARS support. //https://docs.microsoft.com/en-us/sql/relational-databases/native-client/features/using-multiple-active-result-sets-mars?view=sql-server-ver15 var firstkey = new CdKey { Id = Guid.NewGuid().ToString(), //this bug fix solution Name = "asp.net", BuildType = BuildType.Test }; await dbContext.CdKeys.AddAsync(firstkey); await dbContext.SaveChangesAsync(); var keys = dbContext.CdKeys?.Where(k => k.BuildType == BuildType.Test); foreach (var item in keys) { var values = dbContext.keyValues.Where(v => v.KeyId == item.Id).ToList();//InvalidOperationException: //There is already an open DataReader associated with this Command which must be closed first. } }