我正在使用.NET后端开发Azure移动服务。 发布服务时,我开始在Seed方法中遇到问题。
Collection was modified; enumeration operation may not execute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
指向第196行
Line 182: foreach (Service s in ListServices.ToArray())
Line 183: {
Line 184: context.Services.Add(s);
Line 185: }
Line 186: foreach (Client c in ListClient.ToArray())
Line 187: {
Line 188: context.Clients.Add(c);
Line 189: }
Line 190: foreach (Employee e in ListEmployees.ToArray())
Line 191: {
Line 192: context.Employees.Add(e);
Line 193: }
Line 194: foreach (Calendar c in ListCalendar.ToArray())
Line 195: {
Line 196: context.Calendars.Add(c);
Line 197: }
Line 198: //context.Calendars.Add(cal1);
为什么我在这里遇到这个问题,之前几行,我添加了其他对象? 我看到它因为连接字符串而抱怨,所以我继续创建另一个数据库并从其Properties页面复制了wole连接字符串。
仍然没有运气,所以。
当我开始向上下文添加更多对象时,这种情况就开始发生了。 在开始时只有6个左右的对象(不同类型),它们已成功添加到数据库中。
现在已创建架构,但未添加任何数据。 有什么想法吗?
堆栈跟踪:
[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
System.Collections.Generic.Enumerator.MoveNextRare() +11013833
System.Collections.Generic.Enumerator.MoveNext() +64
System.Data.Entity.Core.Objects.EntityEntry.TakeSnapshotOfRelationships() +705
System.Data.Entity.Core.Objects.Internal.EntityWrapperWithoutRelationships`1.TakeSnapshotOfRelationships(EntityEntry entry) +36
System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName) +311
System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity) +327
System.Data.Entity.Internal.Linq.<>c__DisplayClassd.<Add>b__c() +98
System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +355
System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +200
System.Data.Entity.DbSet`1.Add(TEntity entity) +131
adapptacjaService.MobileServiceInitializer.Seed(MobileServiceContext context) in c:\ADAPPTACJA\WindowsUniversal\AdapptacjaUniversal\adapptacjaService\App_Start\WebApiConfig.cs:196
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +302
Microsoft.WindowsAzure.Mobile.Service.ClearDatabaseSchemaAlways`1.InitializeDatabase(TContext context) +173
[InvalidOperationException: Database initialization failed. Could not initialize one or more objects in schema 'adapptacja'. Please ensure that the database connection string is correct. For more details on the error, please see the inner exception.]
Microsoft.WindowsAzure.Mobile.Service.ClearDatabaseSchemaAlways`1.InitializeDatabase(TContext context) +314
System.Data.Entity.Internal.<>c__DisplayClassf`1.<CreateInitializationAction>b__e() +165
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +110
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +660
System.Data.Entity.Database.Initialize(Boolean force) +61
adapptacjaService.WebApiConfig.Register() in c:\ADAPPTACJA\WindowsUniversal\AdapptacjaUniversal\adapptacjaService\App_Start\WebApiConfig.cs:28
adapptacjaService.WebApiApplication.Application_Start() in c:\ADAPPTACJA\WindowsUniversal\AdapptacjaUniversal\adapptacjaService\Global.asax.cs:10
[HttpException (0x80004005): Database initialization failed. Could not initialize one or more objects in schema 'adapptacja'. Please ensure that the database connection string is correct. For more details on the error, please see the inner exception.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9918021
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): Database initialization failed. Could not initialize one or more objects in schema 'adapptacja'. Please ensure that the database connection string is correct. For more details on the error, please see the inner exception.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9931916
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254