Data seeding is a process where you populate the initial set of data into the database. I’ll explain some of the rudimentary steps needed for creating seed data in your app.
Prior to EF Core 2.1, seeding data was not quite straight-forward. However with this version, you can seed data using the following steps:
- Download the necessary packages from NuGet
- Create your Context Class
- Write Seed Data
- Run Migrations
- Apply data changes to your database
Let’s begin…
Download Packages: You need the following packages after you create a new .NET Core console app:
- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
Context Class: Create a new context class which would derive from DbContext. DbContext represents a session with the database and is used to perform CRUD operations against your database. It’s also a combination of Unit of Work and Repository patterns.
public class DataSeedContext : DbContext
{
public DbSet Blog { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"server=obi-oberoi; database=demo.DotNetConf; trusted_connection=true;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(u => u.Url)
.IsRequired();
modelBuilder.Entity().HasData(
new Blog { BlogId = 1, Url = "http://ObiOberoi.com" },
new Blog { BlogId = 2, Url = "http://microsoft.com" },
new Blog { BlogId = 3, Url = "http://cnn.com" },
new Blog { BlogId = 4, Url = "https://yahoo.com"},
new Blog { BlogId = 5, Url = "https://tesla.com" },
new Blog { BlogId = 6, Url = "https://meetup.com/mississauganetug"},
new Blog { BlogId = 7, Url="https://td.com"},
new Blog { BlogId = 8, Url="https://meetup.com/TorontoNETUG"}
);
}
}
Create a Blog class like so:
public class Blog
{
public int BlogId { get; set; }
[Required]
public string Url { get; set; }
}
Be sure to specify the type of database you wish to use. In the example, SQL Server is being used. This is defined in the Context class’s OnConfiguring method.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"server=obi-oberoi; database=demo.DotNetConf; trusted_connection=true;");
}
Run Migration: Just a quick overview of Migrations. Migrations allow you to keep your database in sync with your code. EF Core uses your mapping configurations to produce a snapshot of what your database should look like. Your model may represent tables, its primary keys, foreign keys, indexes, constraints and such.
Whenever you make a change to your POCO class i.e. add or remove a property, you ought to run a PowerShell command like so:
Add-Migration <migration name>
The Add-Migration command computes a database schema from your entity classes and scaffolds a migration with Up and Down methods which consists of the current version of the database to the new version it computes.
Apply Changes to Database: You do this by simply issuing the following command in PowerShell and you are set:
Update-Database
So, this was a quick intro to seeding data into the database.