SetInitializer传递策略时的实体框架错误 [英] Entity Framework error when SetInitializer passed a strategy

查看:213
本文介绍了SetInitializer传递策略时的实体框架错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用实体框架代码,而不用现有的数据库。

我想做的是SetInitializer策略,所以我可以种下一些测试数据。



我在这里跟随示例: / p>

http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-aspnet -mvc-application



所以我添加了一个这样的初始化器:

 code> 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学生{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)}
新学生{F irstMidName =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 $ c> 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
//。删除< System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
}

但是我仍然收到相同的错误。任何想法?



编辑更新:



我刚刚看了并注意到EdmMetadata表未被填充

解决方案

删除数据库并尝试再次运行应用程序。 b $ b

I'm trying to use the Entity Framework code first without an existing database.

I got everything setup fine and it was creating the database.

What I wanted to do was SetInitializer Strategy so I could seed some test data.

I was following the example here:

http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

So I added an Initializer like this:

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();  
        }
    }

I then added:

Database.SetInitializer<MyContext>(new MyInitializer());

to the Application_Start of the Global.ascx file and ran the site. I got:

Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.

I then found in the DataContext I had something like:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions
            .Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
        }

So thinking I found the problem I changed it to:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Conventions
            //.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
        }

However I still get the same error. Any ideas?

Edit Update:

I've just had a look and noticed the EdmMetadata table is not being populated

解决方案

Delete the database and try to run the application again.

这篇关于SetInitializer传递策略时的实体框架错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆