4.3: Seeding the Database
Add Newtonsoft.Json package
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.2" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1"/>
</ItemGroup>Add Seed Class
public class Seed
{
private readonly EFConnectContext _context;
public Seed(EFConnectContext context)
{
_context = context;
}
public void SeedUsers()
{
if (_context.Users.Any()) // check if users are in the databaes
{
_context.Users.RemoveRange(_context.Users); // if so, remove them
_context.SaveChanges();
}
var userData = System.IO.File.ReadAllText("../EFConnect.Data/UserSeedData.json"); // read json
var users = JsonConvert.DeserializeObject<List<User>>(userData); // deserialize to a list of Users
foreach (var user in users)
{
byte[] passwordHash, passwordSalt;
CreatePasswordHash("password", out passwordHash, out passwordSalt);
user.PasswordHash = passwordHash;
user.PasswordSalt = passwordSalt;
user.Username = user.Username.ToLower();
user.LastActive = user.Created;
_context.Users.Add(user);
}
_context.SaveChanges();
}
private void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
{
using (var hmac = new System.Security.Cryptography.HMACSHA512())
{
passwordSalt = hmac.Key;
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
}
}
}Register the Seed Class in Our Services
Add Seeding to the Middleware Pipeline
Run the Project and Seed the Data
Check the Database

Last updated