Where can I put seed code using Entity Framework Core?

c# entity-framework entity-framework-core

Question

I created an ASP.NET Core web application using the Visual Studio wizard, which generated all the DbContext stuff. I am currently trying to implement the Enum Lookup Table Generator package. The documentation says:

Run EnumToLookup.Apply() from your Seed method in either your database initializer or your EF Migrations.

My issue is that my DbContext does not currently use a database initializer.

Where should I run EnumToLookup.Apply()? Do I need to add a database initializer so that I can override the Seed() method where the documentation says to put EnumToLookup.Apply()?

My DbContext looks like this:

namespace WebApplication3.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }

        public DbSet<WebApplication3.Models.Restaurant> Restaurant { get; set; }
        public DbSet<WebApplication3.Models.Customer> Customer { get; set; }
    }
}
1
0
3/5/2018 5:32:04 AM

Accepted Answer

I did some more research and it turns out that while EF6 had the ability to add seed data, EF7 / EF Core 2.0 does not include this ability.

This functionality may be added with the release of EF Core 2.1, but it might not make it in.

1
3/5/2018 5:29:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow