zoukankan      html  css  js  c++  java
  • EntityFramework数据库迁移

    数据库迁移

    1:Enable-Migrations

    2:Add-Migration Student.

    3:update-database

    一般执行以上3个命令都会成功

    第一步:删除迁移命令生成的文件夹:Migrations文件夹,重新生成

    第二步:执行迁移命令: Add-Migration ,例如:有一个Student的实体类(Model) 为了命名规范,我们一般执行迁移命令如下

    Add-Migration Student.

    第三步:执行 Enable-Migrations -Force命令

    第四步:删除Migrations文件夹下面的带Create的文件

    第五步:执行Enable-Migrations命令

    第六步:与第二步执行命令相同,Add-Migration Student

    第七步:提交到数据库执行命令:update-database

    第八步:查看执行添加表字段的脚步命令:update-database -Verbose

    第九步:重新编译解决方案,因为我们执行的命令是修改了debug/relealse 目录下面对应的数据库,所以如果项目中的数据库文件设置为

    资源输出文件,并且设置为总是覆盖的话,则数据库即使执行了增加字段命令,但是还是会被重新覆盖,所以执行完命令成功后复制debug/relealse

    目录下面的数据库文件覆盖到解决方案中的数据库文件,将其覆盖或者设置,并且设置为总是不覆盖的

    使用自动迁移时注意启动项目,最好将数据库连接的字符串连接配置到对应的Models对应的web.config 或者App.config中。

    如model在项目 “PQCC.Models” 则必须在PQCC.Models 项目中的web.config 或者App.config中配置:

    <connectionStrings>
    <add name="BlogDatabase" connectionString="Data Source=.;Initial Catalog=BlogDatabase;UID=sa;PWD=123456;Integrated Security=True" providerName="System.Data.SqlClient"/> </connectionStrings>

    后期发布到正式站点后需要获取自动生成的语句sql脚本。并且更新正式站点数据库

    语法如下: Update-Database -Script -SourceMigration 201610030621164_ModifyScoreReprot

    201411240324186_ModifyScoreReprot 表示当前版本的sql 脚本语句。对应于dbo.__MigrationHistory 表中的MigrationID字段

    如果后期发布到正式站点后需要获取自动生成的语句sql脚本。并且更新正式站点数据库执行 以下语句是

    Update-Database -Script -SourceMigration 201610030621164_ModifyScoreReprot 报以下错误时:

    Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration

    是由于您当前项目Migrations 文件 中不包含201610030621164_ModifyScoreReprot文件。

    而dbo.__MigrationHistory 表中MigrationID字段中却有201610030621164_ModifyScoreReprot的记录,说明其他跟你一起合作开发的同学没有提交项目中的201610030621164_ModifyScoreReprot文件或者是该文件以及被手动删除啦。所以最好将Migrations 文件夹下的所有生成的文件都一次提交上去到svn才是王道

  • 相关阅读:
    Android特色开发(3):Google Map
    Android特色开发(4):桌面组件
    Android用户界面开发(19):ProgressBar
    每日英语:Fewer Foreigners Eye US Graduate Science Programs
    每日英语:My Adventures With Liquid Chicken
    每日英语:Hard Math: Adding Up Just How Little We Actually Move
    每日英语:Thousands of Thumbs Down for Chinese Red Cross
    每日英语:Rescuers Struggle to Reach Quake Victims
    每日英语:When Simplicity Is The Solution
    【转】Matlab图像处理函数:regionprops
  • 原文地址:https://www.cnblogs.com/liyujun1988/p/5929134.html
Copyright © 2011-2022 走看看