2
我試圖在沒有現有數據庫的情況下先使用Entity Framework代碼。SetInitializer通過策略時出現Entity Framework錯誤
我得到了一切安裝好,它正在創建數據庫。
我想要做的是SetInitializer策略,所以我可以播種一些測試數據。
我在這裏下面的例子:
所以我加了這樣一個初始化:
public class MyInitializer : DropCreateDatabaseIfModelChanges<MyContext>
{
protected override void Seed(MyContext context)
{
var students = new List<Student>
{
new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01") },
new Student { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01") },
new Student { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") }
};
students.ForEach(s => context.Students.Add(s));
context.SaveChanges();
}
}
我接着說:
Database.SetInitializer<MyContext>(new MyInitializer());
到的Application_Start
Global.ascx
文件並運行該網站。我得到了:
模型兼容性無法檢查,因爲EdmMetadata類型未包含在模型中。確保IncludeMetadataConvention已被添加到DbModelBuilder約定中。
然後我在DataContext的發現我有這樣的事情:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions
.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
}
這樣想着,我發現我把它改成了問題:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//modelBuilder.Conventions
//.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
}
但是我仍然得到同樣的錯誤。有任何想法嗎?
編輯更新:
我剛剛看了一下,發現該EdmMetadata表未被填充
我有同樣的問題,解決方案是刪除數據庫並再次運行該應用程序。 謝謝 – Ezequiel