InvalidOperationException in Mobile Service Initialization

268 Views Asked by At

I am working on an Azure Mobile Service with .NET backend. I started having problems in the Seed method when publishing the service.

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.

It points to Line 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);

Why am I getting this problem here, when a couple of lines before, I add other objects? I see that it complains because of the connection string, so I went ahead and created another database and copied the wole connection string from its Properties page.

Still no luck, tho.

This started happening, when I started adding more objects to the context. In the beginning with only 6 or so objects (of different types), they got successfully added to the Database.

Right now the schema gets created, but no data is added. Any ideas please?

StackTrace:

[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
0

There are 0 best solutions below