【问题标题】:with asp.net core and ef core when i try to seed there went wrong当我尝试播种时,使用 asp.net core 和 ef core 出错了
【发布时间】:2017-10-05 18:07:34
【问题描述】:

我的 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();
    }
}

当我在 Nuget 包管理中 Update-Database 时:

在类“程序”上调用方法“BuildWebHost”时发生错误。在没有应用程序服务提供商的情况下继续。错误:无法从根提供程序解析范围服务“Blog.DAL.Context.BlogDbContext”。

无法创建“BlogDbContext”类型的对象。将“IDesignTimeDbContextFactory”的实现添加到项目中,或查看https://go.microsoft.com/fwlink/?linkid=851728 了解设计时支持的其他模式。

【问题讨论】:

    标签: asp.net-core ef-core-2.0


    【解决方案1】:

    好吧我看文档解决了,Asp.Net Core 1.x和2.0有些不同;我只是应该在program.cs中写seed方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2021-03-09
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 2016-11-30
      • 2016-12-18
      相关资源
      最近更新 更多