Peter Kellner
Peter Kellner

Reputation: 15488

Problem Seeding Data in EF Core with Identity/Auth Enabled .Net 5

I've created an ASP.NET Core Web App with .net 5 and the React Template and enabled authentication in the VS wizard. The code created for the dbContext is this:

public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
  {
    public ApplicationDbContext(
        DbContextOptions options,
        IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {
    }

I want access to this dbContext in a seedData method I'm calling from Program.cs like this:

var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
   var services = scope.ServiceProvider;
   try { SeedData.Initialize(services); } ...

When I try to get a reference to my Context in SeedData / Initialize, I get a null reference error because of the the second parameters of the constructor missing when instantiating DbContextOptions as follows (null is obviously wrong, but I don't know what I should pass instead)

public static class SeedData
{
    public static void Initialize(IServiceProvider serviceProvider)
    {
        using ApplicationDbContext context = new ApplicationDbContext(
            serviceProvider.GetRequiredService<
                DbContextOptions<ApplicationDbContext>>(),null);

Upvotes: 0

Views: 345

Answers (1)

mohammadmahdi Talachi
mohammadmahdi Talachi

Reputation: 601

Try this one :

At first you have to create scope and get require services from that in program.cs (ApplicationUser inherits from IdentityUser ).

 public static void Main(string[] args)
{
   var host = CreateHostBuilder(args).Build();

   using (var scope = host.Services.CreateScope())
   {
       var services = scope.ServiceProvider;

       var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();

       ApplicationDbContextSeed.SeedDefaultUserAsync(userManager)
         .GetAwaiter().GetResult(;
   }

   host.Run();

}

next you have to create static seeding method in your seeding class for example :

public class ApplicationDbContextSeed
{
    public static async Task SeedDefaultUserAsync(UserManager<ApplicationUser> userManager)
    {
        var user = new ApplicationUser()
        {
            Email = "email",
            UserName = "username",
        };

        await userManager.CreateAsync(user, "password");

   }
}

You can also seed your database in this class

public static async Task SeedSampleDataAsync(ApplicationDbContext context)
{
            // Seed, if necessary
            if (!context.TodoLists.Any())
            {
                context.TodoLists.Add(new TodoList
                {
                    Title = "Shopping",
                    Items =
                    {
                        new TodoItem { Title = "Apples", Done = true },
                        new TodoItem { Title = "Milk", Done = true },
                        new TodoItem { Title = "Bread", Done = true },
                        new TodoItem { Title = "Toilet paper" },
                        new TodoItem { Title = "Pasta" },
                        new TodoItem { Title = "Tissues" },
                        new TodoItem { Title = "Tuna" },
                        new TodoItem { Title = "Water" }
                    }
                });

                await context.SaveChangesAsync();
            }
}

and at last call this method in program.cs

 public static void Main(string[] args)
{
   var host = CreateHostBuilder(args).Build();

   using (var scope = host.Services.CreateScope())
   {
       var services = scope.ServiceProvider;

       var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();

       ApplicationDbContextSeed.SeedDefaultUserAsync(userManager)
         .GetAwaiter().GetResult(;

       var context = services.GetRequiredService<ApplicationDbContext>();

       await ApplicationDbContextSeed.SeedSampleDataAsync(context);
   }

   host.Run();

}

Upvotes: 1

Related Questions