I am trying to implement an in-memory Sqlite database on my C# application to use for testing purposes. I would like to seed the database during startup so that I can control the data used in my Cypress end to end tests. From my research online, I see 2 ways to do this.
The first is to add data to the context using the DbContext.AddRange()
method, as shown in Mircosoft's example here.
The second is within the OnModelCreating() method, where you can add data to a model like this:
modelBuilder.Entity<Book>().HasData(
new Book { BookId = 1, AuthorId = 1, Title = "Hamlet" },
new Book { BookId = 2, AuthorId = 1, Title = "King Lear" },
new Book { BookId = 3, AuthorId = 1, Title = "Othello" }
);
Documentation on Microsoft: https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding
In either case, I believe you also have to use context.Database.EnsureCreated();
, but I am not sure where to put it.
What is the reccommended way to seed my database with test data?