Home > Database > Mysql Tutorial > EF Code First Migrations数据库迁移

EF Code First Migrations数据库迁移

WBOY
Release: 2016-06-07 14:59:48
Original
1367 people have browsed it

1、EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework。 在程序包管理器控制台中执行以下语句,安装EntityFramework。 PM Install-Package EntityFramework 安装成功后,界面提示如下图: 在新建的Portal控制台

1、EF Code First创建数据库

  新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework。

  在程序包管理器控制台中执行以下语句,安装EntityFramework。

PM> Install-Package EntityFramework
Copy after login

  安装成功后,界面提示如下图:

EF Code First Migrations数据库迁移

   在新建的Portal控制台应用程序中添加两个实体类,代码结构如下:

EF Code First Migrations数据库迁移

  其中,类文件PortalContext.cs的代码如下:

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Text;

</span><span>using</span><span> System.Data.Entity;
</span><span>using</span><span> System.Data.Entity.Infrastructure;

</span><span>using</span><span> Portal.Entities;
</span><span>using</span><span> Portal.Mapping;

</span><span>namespace</span><span> Portal
{
    </span><span>public</span> <span>class</span><span> PortalContext : DbContext
    {
        </span><span>static</span><span> PortalContext()
        {
            Database.SetInitializer(</span><span>new</span> DropCreateDatabaseIfModelChanges<portalcontext><span>());
        }

        </span><span>public</span> DbSet<province> Provinces { <span>get</span>; <span>set</span><span>; }
        </span><span>public</span> DbSet<category> Categories { <span>get</span>; <span>set</span><span>; }

        </span><span>protected</span> <span>override</span> <span>void</span><span> OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(</span><span>new</span><span> ProvinceMap());
            modelBuilder.Configurations.Add(</span><span>new</span><span> CategoryMap());
        }
    }
}</span></category></province></portalcontext>
Copy after login

  在静态构造函数中,设置了当数据库模型发生改变时,则删除当前数据库,重建新的数据库。

  代码执行后,生成的数据库:

 EF Code First Migrations数据库迁移

2、EF Code First数据库迁移

2.1、生成数据库

  修改类文件PortalContext.cs的静态构造函数,取消当数据库模型发生改变时删除当前数据库重建新数据库的设置。

<span>static</span><span> PortalContext()
{
    Database.SetInitializer</span><portalcontext>(<span>null</span><span>);
}</span></portalcontext>
Copy after login

   1>、在程序包管理器控制台,执行语句:

PM> Enable-Migrations -EnableAutomaticMigrations
Copy after login

 EF Code First Migrations数据库迁移

  执行成功后,Portal控制台应用程序代码结构中,添加Migrations文件夹,并生成类文件Configuration.cs。

EF Code First Migrations数据库迁移

<span>namespace</span><span> Portal.Migrations
{
    </span><span>using</span><span> System;
    </span><span>using</span><span> System.Data.Entity;
    </span><span>using</span><span> System.Data.Entity.Migrations;
    </span><span>using</span><span> System.Linq;

    </span><span>internal</span> <span>sealed</span> <span>class</span> Configuration : DbMigrationsConfiguration<portal.portalcontext><span>
    {
        </span><span>public</span><span> Configuration()
        {
            AutomaticMigrationsEnabled </span>= <span>true</span><span>;
        }

        </span><span>protected</span> <span>override</span> <span>void</span><span> Seed(Portal.PortalContext context)
        {
            </span><span>//</span><span>  This method will be called after migrating to the latest version.

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

   2>、在程序包管理器控制台,执行语句:

PM> Add-Migration InitialCreate
Copy after login

EF Code First Migrations数据库迁移

  执行成功后,在Migrations文件夹中新增类文件201309201556388_InitialCreate.cs

EF Code First Migrations数据库迁移

  3>、在程序包管理器控制台,执行语句:

PM> Update-Database -Verbose
Copy after login
Copy after login
Copy after login

  执行结果生成与上面一致的数据库

EF Code First Migrations数据库迁移

  4>、在数据库模型中添加City类,执行程序包管理器控制台语句,Migrations文件夹中新增类文件201309201643300_AddCity.cs。

PM> Add-Migration AddCity
Copy after login

  再次执行程序包管理器控制台语句

PM> Update-Database -Verbose
Copy after login
Copy after login
Copy after login

EF Code First Migrations数据库迁移

  Portal控制台应用程序的代码结构:

EF Code First Migrations数据库迁移

  数据库更新成功之后,在数据库中新增表City。

EF Code First Migrations数据库迁移

2.2、版本回溯

  修改数据库中表City,删除其中字段ProvinceNo。在程序包管理器控制台中执行以下两条语句:

PM> Add-Migration ModifyCity
Copy after login

PM> Update-Database -Verbose
Copy after login
Copy after login
Copy after login

  执行成功之后,City表结构修改为:

EF Code First Migrations数据库迁移

  执行程序包管理器控制台语句,进行数据库版本回溯。

PM> Update-Database –TargetMigration:<span>"</span><span>201309201643300_AddCity.cs</span><span>"</span>
Copy after login

2.3、生成数据库版本之间的Sql脚本

  执行程序包管理器控制台语句,生成数据库版本之间的Sql脚本。该操作仅为生成Sql语句,并未在数据库中进行执行。

Update-Database -Script -SourceMigration:<span>"</span><span>201309201643300_AddCity.cs</span><span>"</span> -TargetMigration:<span>"</span><span>201309201708043_ModifyCity.cs</span><span>"</span> 
Copy after login

  其中-TargetMigration在未指定的情况,默认为迁移到最新的版本。

3、EF Code First Migrations语句的其他参数

1>、为指定的DbContext启用数据库迁移

PM> Enable-Migrations -ContextTypeName Portal.PortalContext
Copy after login

2>、设置是否允许自动迁移

Enable-Migrations
Copy after login

生成的Configuration.cs类文件的构造函数

<span>public</span><span> Configuration()
{
      AutomaticMigrationsEnabled </span>= <span>false</span><span>;
}</span>
Copy after login

3>、Enable-Migrations指定项目名称

PM> Enable-Migrations -StartUpProjectName Portal
Copy after login

如果在“Package Manager Console”中选择了默认项目可以不设置“-StartUpProjectName”参数;如果多次执行此命令可以添加-Force参数。

4>、查看所执行的Sql语句 -Verbose指令

Update-Database -Verbose 
Copy after login

4、代码下载

Portal.zip

5、参考资料

http://msdn.microsoft.com/en-US/data/jj591621

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template