痛快-EF实现表自动更新

    public class DbHelper:DbContext
    {
        public DbHelper()
            : base("strConn")
        {
            //自动创建表,如果Entity有改到就更新到表结构
            Database.SetInitializer<DbHelper>(new MigrateDatabaseToLatestVersion<DbHelper, ReportingDbMigrationsConfiguration>());
        }
        public DbSet<Students> Students { get; set; }
    }
    internal sealed class ReportingDbMigrationsConfiguration : DbMigrationsConfiguration<DbHelper>
    {
        public ReportingDbMigrationsConfiguration()
        {
            AutomaticMigrationsEnabled = true;//任何Model Class的修改將會直接更新DB
            AutomaticMigrationDataLossAllowed = true;
        }
    }

 

posted @ 2013-08-03 13:41  痛快  阅读(490)  评论(0编辑  收藏  举报