实体框架4.1代码数据库中的第一个永久表 [英] Entity Framework 4.1 Code First and permanent table in database

查看:86
本文介绍了实体框架4.1代码数据库中的第一个永久表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张表,可以永久保存美国邮政编码和城市名称列表。不幸的是,似乎在EF 4.1中,如果我在开发过程中修改模型,整个数据库需要被删除并重新创建。由于EF中尚未支持迁移,这似乎是一个问题。任何想法?

I have an table that permanently stores a list of zip codes and city names for the US. Unfortunately, it seems that with EF 4.1 if I modify the model during developement, the entire db needs to be droped and recreated. Since migrations are not yet supported in EF, this seems to be a problem. Any ideas?

推荐答案

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

you can use a custome initializer class. In here you can insert all values when database changes happens. This is from Contoso Universty example from msdn.

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


    }
 }

这篇关于实体框架4.1代码数据库中的第一个永久表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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