Better way to Seed a Dot Net Core Entity Framework Database?

.net-core c# entity-framework-core seeding sql-server

Question

I'm working to seed a database I'm creating for a United Way I'm volunteering for. (Check out TapRoot+ if you are interested in volunteering and applying some learning)

Anyways, I'm only seeding one to many fields right now, but I've gotten it to work one table at a time.

public class Seed
{
    private CharEmContext _context;

    public Seed(CharEmContext context)
    {
        _context = context;
    }

    public async Task SeedCounty()
    {
        if (!_context.Counties.Any())
        {
            _context.AddRange(_counties);
            await _context.SaveChangesAsync();
        }
    }...`

and

static List<County> _counties = new List<County>
    {
        new County { Name = "Emmet"},
        new County { Name = "Charlevoix"},
        new County { Name = "Antrim"},
        new County { Name = "Cheboygan"},
        new County { Name = "Otsego"},
        new County { Name = "None"}
    };

But run into trouble when I'm trying to reference the Id's assigned once they are created.

Neither this:

static List<City> _cities = new List<City>
{
   new City { Name = "Alanson", CountyId = _context.Counties.Where(x=>x.Name =="Emmet").Select(x => x.Id).FirstOrDefault()}, ... }

Nor this:

static List<City> _cities = new List<City>
{
    new City { Name = "Alanson", CountyId = _counties.Where(x=>x.Name =="Emmet").Select(x => x.Id).FirstOrDefault()},

works.

For reference I'm creating a service in my startup.cs.ConfigureServices

services.AddTransient();

and calling each add method in the .Configure(Seed Seeder)

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, Seed seeder)
{
    seeder.SeedCounty().Wait();
    seeder.SeedCity().Wait();
    seeder.SeedLocation().Wait();

Definitely need to know how to reference those IDs the DB creates on the dependent tables.

Also curious if I need to seed the linking tables in the many-to-many relationships...

1
0
8/11/2017 2:24:04 PM

Popular Answer

So the key was to create explicit dictionaries:

Dictionary<string, int> County;
    Dictionary<string, int> City;
    Dictionary<string, int> Address;
    Dictionary<string, int> ServiceType;
    Dictionary<string, int> Organization;
    Dictionary<string, int> Contact;`

and populate them within the seed actions:

public async Task SeedCounty()
    {
        if (!_context.Counties.Any())
        {
            _context.AddRange(_counties);
            await _context.SaveChangesAsync();
        }

        County = _context.Counties.ToDictionary(p => p.Name, p => p.Id);
    }

Then to create subsequent lists that reference the ID's that are now seeded, a separate call is made to create each dependent list to be seeded:

 public async Task SeedCity()
    {
        CreateCities();  // Create Dependent List

        if (!_context.Cities.Any())
        {
            _context.AddRange(_cities);  //Seed List
            await _context.SaveChangesAsync();
        }

        City = _context.Cities.ToDictionary(p => p.Name, p => p.Id); //Create Dictionary with New Ids
    }
0
8/11/2017 1:46:09 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow