我遇到数据库初始化问题 - 从不调用种子方法。类似的代码在另一个项目中工作,所以我很困惑为什么他们这次不工作。
这是我的代码:
RecipeContext.cs
public class RecipeContext : DbContext
{
public RecipeContext() : base("DefaultConnection")
{
}
public DbSet<Recipe> Recipes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
public static RecipeContext Create()
{
return new RecipeContext();
}
}
RecipeInitialiser.cs
public class RecipeInitialiser : DropCreateDatabaseAlways<RecipeContext>
{
protected override void Seed(RecipeContext context)
{
var recipes = new List<Recipe>
{
new Recipe {
Name = "Spag Bol",
Ingredients = "1. This\n2. That\n3. This too",
Method = "1. Do this\n2. Do that\n3. And this too",
PrepTime = new TimeSpan(0, 45, 0),
CookTime = new TimeSpan(1, 45, 0),
Difficulty = 1,
Serves = 6,
DateCreated = DateTime.Now
},
};
recipes.ForEach(r => context.Recipes.Add(r));
context.SaveChanges();
base.Seed(context);
}
}
的Web.config
<entityFramework>
<contexts>
<context type="WebRecipes.DataLayer.RecipeContext, WebRecipes.DataLayer">
<databaseInitializer type="WebRecipes.DataLayer.Models.RecipeInitializer, WebRecipes.DataLayer" />
</context>
</contexts>
...other_settings
</entityframework>
答案 0 :(得分:2)
好像你有一个错字。
您的初始化程序类名称是
RecipeInitialiser
在配置中你有
<databaseInitializer type="WebRecipes.DataLayer.Models.RecipeInitializer, WebRecipes.DataLayer" />