- When I have fixed data that I want to insert with a migration, I put the inserts directly in the Up() migration using calls to
Sql("Insert ...")
. See the note halfway down this page: how to insert fixed data - You prevent duplicates in the Seed method by calling the AddOrUpdate overload that takes an identifier expression specifying the natural key – see this answer and this blog entry.
- Primary keys that are integers are created as identity fields by default. To specify otherwise use the
[DatabaseGenerated(DatabaseGeneratedOption.None)]
attribute
I think this is a good explanation of Initializer and Seed methods
Here is an example of how to use the AddOrUpdate method:
foreach(var enumValue in Enum.GetValues(typeof(Access.Level)))
{
context.Access.AddOrUpdate(
x => x.Name, //the natural key is "Name"
new Access { AccessId = ((int)enumValue), Name = enumValue.ToString() }
);
}