seeding a Database

 

Creating Models and DBContext



seeding a database

public class DemoContext:DbContext

{

  public DbSet<RegistrationModel> Registrations {get;set;} //<> ive heared this is used for generic types


  public DemoContext(DbContextOptions<DemoContext>options):base(options)

  {

    

  }


   public Task Seed()

   {

       this.Database.EnsureCreated();

       if(!this.Registrations.Any())

       {

         var fake = new FakeData()

         var registrations = await fake.GetRegistrations();

         await this.Registrations.AddRangeAsync(registrations);

         await this.SaveChangesAsync();


       }

   }


}

and in the startup.cs do the following thing in configure.


Comments