我想用默认数据为数据库播种,但抛出异常。
我在 DAL 命名空间中添加了这个初始化类
public class MyModelInitialise : DropCreateDatabaseIfModelChanges<MyModelContext>
{
protected override void Seed(MyModelContext context)
{
base.Seed(context);
var MyMarks = new List<MyMark>
{
new Mark{ Name="Mark1", Value="250"},
new Mark{ Name="Mark2", Value="350"},
new Mark{ Name="Mark3", Value="450"}
};
Marks.ForEach(bm => context.Marks.Add(bm));
context.SaveChanges();
}
}
我将此初始化程序添加到应用程序启动
protected void Application_Start()
{
Database.SetInitializer<MyModelContext>(new MyModelInitialise());
}
调用它时出现以下错误
Model compatibility cannot be checked because the DbContext instance was not created
using Code First patterns. DbContext instances created from an ObjectContext or using
an EDMX file cannot be checked for compatibility.