Entity Framework error when SetInitializer passed a strategy

时光毁灭记忆、已成空白 提交于 2019-12-11 04:09:39

问题


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


回答1:


Delete the database and try to run the application again.



来源:https://stackoverflow.com/questions/7085957/entity-framework-error-when-setinitializer-passed-a-strategy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!