Dave Paquette

Caffeine Driven Development

Introducing Entity Framework Seeder

June 2, 2014

In a previous blog post, we discussed a fairly straight forward mechanism for seeding an Entity Framework database from a CSV file using the CsvHelper package.

Since then, I have created a package that simplifies this process even further.

Install-Package EntityFramework.Seeder.EF6

Seeding Simple Entities

Assume we are working with a very a very simple lookup entity like a  Country:

public class Country
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}
public class Country
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

 

Create a CSV file with a Code and Name column. We do not need an ID column because the ID will be auto-generated by the database.

Code,Name
CA,Canada
USA,United States
etc...
Code,Name
CA,Canada
USA,United States
etc...

 

Include the CSV file as an embedded resource in your C# project. In your seed method call the SeedFromResource extension method on the Countries DbSet, specifying the name of the embedded resource and the property that uniquely identifies a Country. The unique property is required to ensure that the same Country is not added to the database a second time if the Country already exists in the database.

context.Countries.SeedFromResource("MyProject.countries.csv", c => c.Code);
context.Countries.SeedFromResource("MyProject.countries.csv", c => c.Code);

Seeding Entities with Relationships

Let’s extend the model to include the concept of Provinces / States.

public class Country
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public virtual IList<ProvinceState> ProvinceStates { get; set; }
}
 
public class ProvinceState
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
 
    public virtual Country Country { get; set; }
}
public class Country
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public virtual IList<ProvinceState> ProvinceStates { get; set; }
}

public class ProvinceState
{
    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    public virtual Country Country { get; set; }
}

 

Now when we are loading Provinces / States we need to connect them with correct Country. To do this, define a CSV file with a Code and Name column and an additional CountryCode column. The CountryCode column will be used to find the correct Country for the Province / State.

CountryCode,Code,Name
CA,SK,Saskatchewan
CA,AB,Alberta
US,AZ,Arizona
US,AR,Arkansas
US,CA,California
etc...
CountryCode,Code,Name
CA,SK,Saskatchewan
CA,AB,Alberta
US,AZ,Arizona
US,AR,Arkansas
US,CA,California
etc...

 

When Seeding the provinces, specify an additional CsvColumnMapping parameter. This parameter is used to tell Entity Framework Seeder how to find the correct Country based on the CountryCode column.

context.Countries.SeedFromResource("MyProject.countries.csv", c => c.Code);
context.SaveChanges();
context.ProvinceStates.SeedFromResource("MyProject.provincestates.csv", p => p.Code,
        new CsvColumnMapping<ProvinceState>("CountryCode", (state, countryCode) =>
            {
                state.Country = context.Countries.Single(c => c.Code == countryCode);
            })
         );   
context.Countries.SeedFromResource("MyProject.countries.csv", c => c.Code);
context.SaveChanges();
context.ProvinceStates.SeedFromResource("MyProject.provincestates.csv", p => p.Code,
        new CsvColumnMapping<ProvinceState>("CountryCode", (state, countryCode) =>
            {
                state.Country = context.Countries.Single(c => c.Code == countryCode);
            })
         );   

 

Provide Feedback / Contribute

You can find Entity Framework Seeder on GitHub and on Nuget.

2 thoughts on “Introducing Entity Framework Seeder

  1. Pingback: Dew Drop – June 3, 2014 (#1789) | Morning Dew

  2. Sithelo says:

    Thanks for your simple tutorial. How do you deal with nullable propertes in my class? e.g public DateTime? modifiedDate {get; set;}. I get an error Type is not marked as serializable.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

Current day month ye@r *