我的 startup.cs 是
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
SeedData.Seed(app);
}
我的种子课程是:
public static class SeedData
{
public static void Seed(IApplicationBuilder app)
{
var _dbContext= app.ApplicationServices.GetRequiredService<BlogDbContext>();
_dbContext.Database.EnsureDeleted();
_dbContext.Add<User>(new User { UserName = "Niko", Password ="123",EmailAddress="nikozhao5456@gmail.com",UserType= Models.User.Type.Visitor,RegistDate=System.DateTime.Now});
_dbContext.Add<Admin>(new Admin{EmailAddress="lovezgd888@163.com",Password="123"});
_dbContext.SaveChanges();
}
}
当我Update-Database
在 Nuget 包管理中时:
在类“程序”上调用方法“BuildWebHost”时发生错误。在没有应用程序服务提供商的情况下继续。错误:无法从根提供程序解析范围服务“Blog.DAL.Context.BlogDbContext”。
和
无法创建“BlogDbContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或查看https://go.microsoft.com/fwlink/?linkid=851728了解设计时支持的其他模式。