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 == "[email protected]");
        if (douglas == null)
        {
            _context.Patients.Add(
                new Patient
                {
                    Name = "Douglas",
                    Surname = "Adams",
                    EmailAddress = "[email protected]"
                });
        }

        var asimov = _context.Patients.FirstOrDefault(p => p.EmailAddress == "[email protected]");
        if (asimov == null)
        {
            _context.Patients.Add(
                new Patient
                {
                    Name = "Isaac",
                    Surname = "Asimov",
                    EmailAddress = "[email protected]"
                });
        }
        _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)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This is actually a problem related directly to EF, since ABP framework has no effect on seed code. But I will try to help you. Your code seems correct at first sight. Maybe you missed to create InitialPatientCreator and use Create method in your seed code. You can call if from InitialDbBuilder like this: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.EntityFramework/Migrations/Seed/InitialDbBuilder.cs#L22">https://github.com/aspnetzero/aspnet-ze ... der.cs#L22</a>

  • User Avatar
    0
    skinnerjames created

    Thank you! The issue was the missing line in InitialDBBuilder.cs file "new InitialPatientCreator(_context).Create();".