Base solution for your next web application
Open Closed

Seed Method not working. #860


User avatar
0
skinnerjames created

I am trying to add a "patient" registration form by following the steps in the Acme.PhoneBook tutorial and am getting stuck on several points. The first point is using the Seed Method to import sample data into the DB. Here is my InitialPatientCreator.cs code.

using System.Collections.Generic;
using System.Linq;
using AHPClaims.AHPClaims.EntityFramework;
using AHPClaims.AHPClaims.Patients;

namespace AHPClaims.AHPClaims.Migrations.Seed
{
public class InitialPatientCreator
{
private readonly AHPClaimsDbContext _context;

    public InitialPatientCreator(AHPClaimsDbContext context)
    {
        _context = context;
    }

    public void Create()
    {
        var douglas = _context.Patients.FirstOrDefault(p => p.EmailAddress == "douglas.adams@fortytwo.com");
        if (douglas == null)
        {
            _context.Patients.Add(
                new Patient
                {
                    Name = "Douglas",
                    Surname = "Adams",
                    EmailAddress = "douglas.adams@fortytwo.com"
                });
        }

        var asimov = _context.Patients.FirstOrDefault(p => p.EmailAddress == "isaac.asimov@foundation.org");
        if (asimov == null)
        {
            _context.Patients.Add(
                new Patient
                {
                    Name = "Isaac",
                    Surname = "Asimov",
                    EmailAddress = "isaac.asimov@foundation.org"
                });
        }
        _context.SaveChanges();
    }
}

}

Up to this point everything has worked as it should. When I run Update-Database in package manager nothing imports into DB. Does something in this file appear incorrect? Thanks in advance.


2 Answer(s)