0
votes

My startup.cs is

 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);
        }

and my seed class is :

 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="[email protected]",UserType= Models.User.Type.Visitor,RegistDate=System.DateTime.Now});

        _dbContext.Add<Admin>(new Admin{EmailAddress="[email protected]",Password="123"});

        _dbContext.SaveChanges();
    }
}

when I Update-Database in the Nuget Package Manage :

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Cannot resolve scoped service 'Blog.DAL.Context.BlogDbContext' from root provider.

and

Unable to create an object of type 'BlogDbContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

1

1 Answers

0
votes

Well Ive solve it by watching the docs,There is something different between Asp.Net Core 1.x and 2.0;I just should write the seed method in the program.cs