Getting Started

Initializer

The easiest way to get started is to use the implementations of IDatabaseInitializer in the CodeFirstSeeder.Xml namespace. These have the same name and are used just as the initializers included with EF.
  • CreateDatabaseIfNotExists
  • DropCreateDatabaseAlways
  • DropCreateDatabaseIfModelChanges
For more information on the classes these are derived from, see http://msdn.microsoft.com/en-us/library/gg696142(v=VS.103).aspx.

Use your preferred Initializer like so (in Global.asax or other initialziation code):
Database.SetInitializer( new CodeFirstSeeder.Xml.DropCreateDatabaseAlways<MyContext>() ); 

XML file

Create an XML file in your project with the same name as your DbContext class. For instance, if your context class name is MyEntities, name the XML file MyEntities.xml. Set the "Copy to Output Directory" propety to "Copy if newer" or "Copy always".

If we have these entities in our data model:
public class Location
{
     public int Id {get; set;}
     public string City {get; set;}
     public virtual ICollection<User> Users {get; set;}
}

public class User
{
     public int Id {get; set;}
     public string Name{get; set;}
     public int Age {get; set;}
     public virtual Location Location {get; set;}
     public virtual ICollection<Role> Roles {get; set;}
}

public class Role
{
     public int Id {get; set;}
     public string Name{get; set;}
     public virtual ICollection<User> Users {get; set;}
}


and the following context class:
public class MyEntities : DbContext
{
    public DbSet<Location> Locations {get; set;}
    public DbSet<User> Users {get; set;}
    public DbSet<Role> Roles {get;set;}
}

MyEntities.xml could look something like this (Roles will be implemented in next section)
<?xml version="1.0" encoding="utf-8"?>
<DbSets>
    <Locations> <!-- Name of DbSet -->
        <Location> <!-- 1st entity of DbSet -->
            <City>Chicago</City> <!-- Simple property -->
            <Users> <!-- Navigation (Collection) property -->
                <User> <!-- 1st entity in collection of Users -->
                    <Name>Bob</Name>
                    <Age>41</Age>
                </User>
                <User> <!-- 2nd entity in collection of Users -->
                    <Name>Sue</Name> 
                    <Age>29</Age>
                </User>
            </Children>
        </Location>
        <Location> <!-- 2nd entity of DbSet -->
            <City>Boston</City>
            <Users>
                <User>
                    <Name>Mary</Name> 
                    <Age>35</Age>
                </User>
            </Users>
        </Location>
    </Locations>
<!-- more DbSets can be added -->
</DbSets>


If you only have simple one-to-many releationships, that's all you need to start seeding with XML. With this configuration, everytime EF recreates the database, it will populate it with the data in the XML file.

Reusing Entites

Many-to-many releationships require a little extra handling because there is no way to express them in a simple XML tree. Since Role has a many-to-many relationship with User, you can define the data like this:
<?xml version="1.0" encoding="utf-8"?>
<DbSets>
    <Roles>
         <Role key="admin">
            <Name>Administrator</Name>
         </Role>
         <Role key="user">
            <Name>User</Name>
         </Role>
     </Roles>
    <Locations>
        <Location>
            <City>Chicago</City>
            <Users>
                <User>
                    <Name>Bob</Name>
                    <Age>41</Age>
                    <Roles>
                        <Role usekey="admin" />
                        <Role usekey="user" />
                    </Roles>
                </User>
                <User>
                    <Name>Sue</Name> 
                    <Age>29</Age>
                    <Roles>
                        <Role usekey="user" />
                    </Roles>
                </User>
            </Children>
        </Location>
        <Location>
            <City>Boston</City>
            <Users>
                <User>
                    <Name>Mary</Name> 
                    <Age>35</Age>
                    <Roles>
                        <Role usekey="admin" />
                        <Role usekey="user" />
                    </Roles>
                </User>
            </Users>
        </Location>
    </Locations>
<!-- more DbSets can be added -->
</DbSets>

Here, we are adding to 2 DbContexts, Roles and Locations. Any time you define a "key" attribute for an entity, you can reuse that object with "usekey" later on. "usekey" tells Code First Seeder to use an existing entity instead of creating a new one. Keys are particularly useful for many-to-many relationships, but can be used for other situations.

Hopefully that will get you started. Download the source code for a working example.

More documentation coming soon.

Last edited Jan 16, 2012 at 10:58 AM by brenttaylor, version 6

Comments

No comments yet.