EntityFramework 数据库的迁移

第一步:在程序包管理器控制台里: Enable-Migrations -ProjectName EF所在的项目名称

第二步:运行后会在字段生成Migrations文件夹,Migrations->Configuration.cs 类里把AutomaticMigrationsEnabled改为true(即设为model有改动自动更新数据库)

这里可以直接用命令更新数据库

 internal sealed class Configuration : DbMigrationsConfiguration<FoundationDataTool.Models.DB>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(FoundationDataTool.Models.DB context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }

然后执行Update-DataBase -ProjectName VerifySystem.Domain  (dbcontext的文件)

如果数据实体的属性改变 使用如下命令进行数据库的迁移:

  Add-Migration updateuser

  Update-DataBase -ProjectName MedicalInsurance.Domain 

 

 

使用ef  连接mysql的时候需要注意的问题:

1.使用命令来创建数据库,

    Enable-Migrations -ProjectName MedicalInsurance.Domain(此生生migration文件修改如上)    

    Update-DataBase -ProjectName MedicalInsurance.Domain 

2.使用命令来迁移数据库,

   Add-Migration updateuser

  Update-DataBase -ProjectName MedicalInsurance.Domain 

 其中在执行add命令的时候出现错误:No MigrationSqlGenerator found for provider 'MySql.Data.MySqlClient'. Use the SetSqlGenerator method in the target migrations configuration class to register additional SQL generators.

参考在http://stackoverflow.com/questions/15142841/no-entity-framework-provider-found-for-mysql-data-mysqlclient-ado-net-provider找到了答应,需要在Context指定mySql的配置文件

在context文件上加一个注解

  [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]

 

posted @ 2016-03-14 12:27  nele  阅读(745)  评论(0编辑  收藏  举报