chobo2
chobo2

Reputation: 85765

Services.CreateScope() Missing in Asp.net Core 2.1

What replaced Services.CreateScope() in 2.1?

I am trying to follow this tutorial but it seems like stuff has changed quite a bit

https://dotnetthoughts.net/seed-database-in-aspnet-core/

Upvotes: 7

Views: 7291

Answers (3)

Kirk Hawley
Kirk Hawley

Reputation: 341

I had the same problem with the ASP.NET Core MVC with Entity Framework Core tutorial, written for .NET Core 2, trying to run it with .NET Core 2.1. It worked fine once I added

using Microsoft.Extensions.DependencyInjection;

to the problem file, in my case Program.cs.

Upvotes: 21

Asif Iqbal
Asif Iqbal

Reputation: 543

public static void Main(string[] args)
{
        var host = CreateWebHostBuilder(args).Build();
        using (var scope = host.Services.CreateScope())
        {
            try
            {
                //
            }
            catch (System.Exception ex)
            {
                ///
            }
        }
        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

Upvotes: 0

You should replace this line of code:

var host = BuildWebHost(args);

with this

var host = CreateWebHostBuilder(args).Build();

Upvotes: 6

Related Questions