我正在构建一个Asp.Net核心应用程序并试图为数据库播种。我已经编写了在Startup.cs Configure方法中启动种子的逻辑。当我输入断点并按F5时,我无法调试。它似乎达到了断点。任何人都可以指出我所缺少的东西。我的种子逻辑也存在任何问题。如果你可以看到有一个对context.EnsureSeedData(context)的调用。应用程序似乎断开连接并收到以下错误消息
无法访问此站点。本地主机拒绝连接。
我正在使用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 = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 1 },
new Customers { FirstName = "Jack", LastName = "Robinson", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Dan", LastName = "Cruise", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Tom", LastName = "Menon", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 1 },
new Customers { FirstName = "Mike", LastName = "Tyson", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Ben", LastName = "Jones", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Mark", LastName = "Foreman", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Henry", LastName = "Canvedalle", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Blake", LastName = "Walter", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "David", LastName = "Beckham", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "Joe", LastName = "Strand", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "Jammie", LastName = "Oliver", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Ben", LastName = "Affleck", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 });
context.SaveChanges();
}
}
答案 0 :(得分:0)
要在数据库中播种数据,最好执行以下操作:
<强> Startup.cs 强>
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context)
{
...
app.UseMvc();
//Init the database
DbInitializer.Initialize(context);
}
DbInitializer.cs(将其放在上下文类旁边的Data文件夹中)
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 = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 1 },
new Customers { FirstName = "Jack", LastName = "Robinson", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Dan", LastName = "Cruise", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Tom", LastName = "Menon", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 1 },
new Customers { FirstName = "Mike", LastName = "Tyson", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Ben", LastName = "Jones", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Mark", LastName = "Foreman", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 2 },
new Customers { FirstName = "Henry", LastName = "Canvedalle", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Blake", LastName = "Walter", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "David", LastName = "Beckham", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "Joe", LastName = "Strand", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 4 },
new Customers { FirstName = "Jammie", LastName = "Oliver", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 },
new Customers { FirstName = "Ben", LastName = "Affleck", Gender = Gender.Male, Email = "ranjit.menon9@gmail.com", Address = "ABC Farms", City = "London", StateId = 3 });
context.SaveChanges();
}
}
}