New development has been moved to github: https://github.com/ebrenttaylor/code-first-seeder

Project Description

Entity Framework Code First Seeder populates newly created databases with seed data from a simple XML file. You'll no longer have to create custom implementations of IDatabaseInitializer to hard code your seed entities.

It's developed in C# and currently compatible with .NET 4.0 and Entity Framework 4.1 & 4.2.

Download and install with NuGet

PM> Install-Package CodeFirstSeeder
or search for CodeFirstSeeder in Package Manager

Old Way

Let's say we want add a few seed records to the simplest of data models. Here's the standard way to go about it with Entity Framework Code First:

Database.SetInitializer(new CustomDatabaseInitalizer>());

public class CustomDatabaseInitalizer : DropCreateDatabaseAlways<MyContext>
{
    protected override void Seed(MyContext context)
    {
         Role adminRole = new Role {Name = "Admin"};
         Role userRole = new Role {Name = "User"};

         context.Roles.Add( adminRole );
         context.Roles.Add( userRole );

         Location boston = new Location{ City = "Boston"};
         Location chicago = new Location {City = "Chicago"};

         User mary = new User{Name = "Mary"};
         mary.Roles.Add( adminRole);
         mary.Roles.Add(userRole);

         boston.Users.Add(mary);

         User bob = new User {Name = "Bob"};
         bob.Roles.Add(userRole);

         boston.Users.Add(bob);

         User tom = new User {Name = "Tom"};
         tom.Roles.Add(adminRole);
         tom.Roles.Add(userRole);

         chicago.Users.Add(tom);

         context.Locations.Add(boston);
         context.Locations.Add(chicago);    
    }
}

We've added 2 Locations, 3 Users, 2 Roles and hooked up all of the relationships. This method isn't terrible, but it's hard coded into our application. It's also cumbersome to manage and difficult to swap out if wanted to load an entirely different data set.

New Way with Code First Seeder

Database.SetInitializer( new CodeFirstSeeder.Xml.DropCreateDatabaseAlways<MyContext>() );


Now we just create a simple XML file to describe the data we want to insert.
<?xml version="1.0" encoding="utf-8"?>

<DbSets>
  <Roles>
    <Role key="admin">
      <Name>Admin</Name>
    </Role>
    <Role key="user">
      <Name>User</Name>
    </Role>
  </Roles>

  <Locations>
    <Location>
      <City>Boston</City>
      <Users>
        <User>
          <Name>Mary</Name>
          <Roles>
            <Role usekey="admin" />
            <Role usekey="user" />
          </Roles>
        </User>
        <User>
          <Name>Bob</Name>
          <Roles>
            <Role usekey="user" />
          </Roles>
        </User>
      </Users>
    </Location>
    <Location>
      <City>Chicago</City>
      <Users>
        <User>
          <Name>Tom</Name>
          <Roles>
            <Role usekey="admin" />
            <Role usekey="user" />
          </Roles>
        </User>
      </Users>
    </Location>
  </Locations>

</DbSets>

Note: the key attributes are only nescessary for many-to-many relationships

No other code is required! Code First Seeder will match the XML nodes to your entities and their properties.

Last edited May 3, 2013 at 1:41 AM by brenttaylor, version 7