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))]

 

转载于:https://www.cnblogs.com/nele/p/5275254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值