2016-11-23 14 views
0

我正在構建一個Asp.Net核心應用程序並嘗試播種數據庫。我已經編寫了在我的Startup.cs Configure方法中啓動種子的邏輯。當我放置一個斷點並按下F5時,我無法調試。它似乎達到了斷點。任何人都可以指出我錯過了什麼。我的種子邏輯也有問題嗎?如果你可以看到有一個對context.EnsureSeedData(context)的調用。該應用程序似乎disonnect並獲得以下錯誤信息無法調試Startup.cs中的配置方法

此網站無法到達.localhost拒絕連接。

我使用IISExpress

Startup.cs文件中配置方法

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) 
     { 


      using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) 
      { 
       var context = serviceScope.ServiceProvider.GetService<CustomerOrderEntities>(); 
       context.Database.Migrate(); 
       context.EnsureSeedData(context); 
      } 


      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { 
        HotModuleReplacement = true 
       }); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 

       routes.MapSpaFallbackRoute(
        name: "spa-fallback", 
        defaults: new { controller = "Home", action = "Index" }); 
      }); 

      // If you want to dispose of resources that have been resolved in the 
      // application container, register for the "ApplicationStopped" event. 
      appLifetime.ApplicationStopped.Register(() => this.ApplicationContainer.Dispose()); 
     } 

EnsureSeedData方法

public void EnsureSeedData(CustomerOrderEntities context) 
     { 



      if (!context.Customers.Any()) 
      { 

       context.State.AddRange(
        new State { Abbreviation = "AL", Name = "Alabama" }, 
        new State { Abbreviation = "AK", Name = "Alaska" }, 
        new State { Abbreviation = "AZ", Name = "Arizona" }, 
        new State { Abbreviation = "AR", Name = "Arkansas" }, 
        new State { Abbreviation = "OH", Name = "Ohio" }); 
       context.SaveChanges(); 

       context.Customers.AddRange(
        new Customers { FirstName = "Jade", LastName = "Lawrence", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 1 }, 
        new Customers { FirstName = "Jack", LastName = "Robinson", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
        new Customers { FirstName = "Dan", LastName = "Cruise", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
        new Customers { FirstName = "Tom", LastName = "Menon", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 1 }, 
        new Customers { FirstName = "Mike", LastName = "Tyson", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
        new Customers { FirstName = "Ben", LastName = "Jones", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
        new Customers { FirstName = "Mark", LastName = "Foreman", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
        new Customers { FirstName = "Henry", LastName = "Canvedalle", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
        new Customers { FirstName = "Blake", LastName = "Walter", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
        new Customers { FirstName = "David", LastName = "Beckham", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
        new Customers { FirstName = "Joe", LastName = "Strand", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
        new Customers { FirstName = "Jammie", LastName = "Oliver", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
        new Customers { FirstName = "Ben", LastName = "Affleck", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }); 

       context.SaveChanges(); 
      } 
} 
+0

種子初始化加入的配置方法結束Startup.cs文件:https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/working-with-sql – Alexan

回答

0

種子在你的數據庫您做的更好以下數據:

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context) 
{ 
    ... 

    app.UseMvc(); 

    //Init the database 
    DbInitializer.Initialize(context); 
} 

DbInitializer.cs(把這個數據文件夾的上下文類旁)

public static class DbInitializer 
{ 

    public static void Initialize(ApplicationDbContext context) 
    { 
     EnsureSeedData(context); 
    } 

    private void EnsureSeedData(CustomerOrderEntities context) 
    { 

     if (!context.Customers.Any()) 
     { 

      context.State.AddRange(
       new State { Abbreviation = "AL", Name = "Alabama" }, 
       new State { Abbreviation = "AK", Name = "Alaska" }, 
       new State { Abbreviation = "AZ", Name = "Arizona" }, 
       new State { Abbreviation = "AR", Name = "Arkansas" }, 
       new State { Abbreviation = "OH", Name = "Ohio" }); 
      context.SaveChanges(); 

      context.Customers.AddRange(
       new Customers { FirstName = "Jade", LastName = "Lawrence", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 1 }, 
       new Customers { FirstName = "Jack", LastName = "Robinson", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
       new Customers { FirstName = "Dan", LastName = "Cruise", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
       new Customers { FirstName = "Tom", LastName = "Menon", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 1 }, 
       new Customers { FirstName = "Mike", LastName = "Tyson", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
       new Customers { FirstName = "Ben", LastName = "Jones", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
       new Customers { FirstName = "Mark", LastName = "Foreman", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 2 }, 
       new Customers { FirstName = "Henry", LastName = "Canvedalle", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
       new Customers { FirstName = "Blake", LastName = "Walter", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
       new Customers { FirstName = "David", LastName = "Beckham", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
       new Customers { FirstName = "Joe", LastName = "Strand", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 4 }, 
       new Customers { FirstName = "Jammie", LastName = "Oliver", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }, 
       new Customers { FirstName = "Ben", LastName = "Affleck", Gender = Gender.Male, Email = "[email protected]", Address = "ABC Farms", City = "London", StateId = 3 }); 

      context.SaveChanges(); 
     } 
    } 
} 
+0

但我無法調試應用程序本身。我得到一個錯誤消息當我F5我的Visual Studio 2015應用程序時,連接被拒絕 – Tom

+0

另外我沒有看到你有什麼建議,因爲我有我的Context類中的EnsureSeedData並從我的啓動文件調用它的差異 – Tom

+0

這是真的,但它更乾淨:)。關於視覺工作室:因爲我正在使用視覺代碼,所以我沒有答案 – Wouter