using (var context = new ApplicationDbContext()) This method will create the database and apply any seed data that you have defined in your DbContext class. One way to seed data in EF Core is by using the () method in your DbContext class. The OnConfiguring method is overridden to specify the database to use (an SQLite database with the file name AuthorsData.db). In this example, the OnModelCreating method is overridden to seed the Authors table with one row of data. OptionsBuilder.UseSqlite("Data Source=AuthorsData.db") Protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) Protected override void OnModelCreating(ModelBuilder modelBuilder) Here's an example of how you might use EF Core to apply seed data to an SQLite database: public class ApplicationDbContext : DbContext So, seeding forms part of the model configuration. OnModelCreatingĪs of version 2.1, Entity Framework Core has a formal API for applying seed data to the database as part of your migration - the HasData method of the EntityTypeBuilder method, which is exposed by the ModelBuilder.Entity method, accessible in the OnModelCreating method of the DbContext class. This data serves as a baseline and can be used for testing, and development, and to provide some context for the application that will be built on top of the database. Applying seed data to a database refers to the process of inserting initial data into a database, usually when the database is first created.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |