实体框架4.1代码数据库中的第一个和永久表

时间:2011-07-30 06:20:17

标签: c# entity-framework asp.net-mvc-3 orm

我有一张表永久存储美国的邮政编码和城市名称列表。不幸的是,如果我在开发期间修改模型,似乎使用EF 4.1,整个数据库需要进行droped并重新创建。由于EF尚未支持迁移,因此这似乎是一个问题。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您可以使用自定义初始化程序类。在这里,您可以在发生数据库更改时插入所有值。这是来自msdn。

的Contosouniversity示例
    public class SchoolInitializer : DropCreateDatabaseIfModelChanges<SchoolContext>
        {
            protected override void Seed(SchoolContext 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();


    }
 }

答案 1 :(得分:0)

实际上先CTP version of migrations is already available,但它仍然只是预览。