Entity Framework 5.0系列之Code First数据库迁移

来源:互联网 发布:mac好玩的小游戏 编辑:程序博客网 时间:2024/05/21 23:21

我们知道无论是“Database First”还是“Model First”当模型发生改变了都可以通过Visual Studio设计视图进行更新,那么对于Code First如何更新已有的模型呢?今天我们简单介绍一下Entity Framework的数据迁移功能。

Entity Framework配置

在开始今天的话题之前先来看一下Entity Framework的配置,因为有很多朋友因为配置文件的问题造成“Migrations”命令执行失败。

在建立一个应用程序之后我们可以通过在项目上右键“Nuget Packages Manage”进行EF包安装或者直接在“Package Manager Console”中输入“Install-Package EntityFramework”命令进行Entity Framework包安装。这个过程不仅下载并引入了EF框架相关程序集还进行了包的配置和应用用程序配置。下面是这两个配置文件:

packages.config文件:

[csharp] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <packages>  
  3.   <package id="EntityFramework" version="5.0.0" targetFramework="net45" />  
  4. </packages>  


App.config文件:

[csharp] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  6.   </configSections>  
  7.   <startup>  
  8.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  9.   </startup>  
  10.   <entityFramework>  
  11.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
  12.       <parameters>  
  13.         <parameter value="v11.0" />  
  14.       </parameters>  
  15.     </defaultConnectionFactory>  
  16.   </entityFramework>  
  17. </configuration>  


 

packages.config内容比较简单,首先是EF自身版本,然后在安装过程中根据当前应用的.NET Framework版本配置了“targetFramework”,因为不同的.NET Framework版本对应的EF程序集不同,这在安装过程中会自动识别并配置。

App.config中自动添加了“entityFramework”配置节,在EF包安装过程中自动根据当前环境配置了“defaultConnectionFactory”, “defaultConnectionFactory”是EF默认的连接配置,只有在没有配置连接字符串时生效。在上一篇文章中我们提到如果不进行连接字符串配置EF会自动识别并创建数据库到“.\SQLEXPRESS”或者“LocalDb”,事实上就是通过这里识别的,可以看出我机器上没有“.\SQLEXPRESS”就自动使用了“LocalDb”,配置默认连接到“LocalDbConnectionFactory”。

在本例中我们需要将连接指向“.\SQL2008”上,有两种方式建立自己的连接:一种是直接配置“defaultConnectionFactory”,将默认生成的“System.Data.Entity.Infrastructure.LocalDbConnectionFactory”改成“System.Data.Entity.Infrastructure.SqlConnectionFactory”,然后在参数中编写我们的连接字符串(注意默认生成的数据库名为“项目名称.数据库上下文名称”)。

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  6.   </configSections>  
  7.   <startup>  
  8.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  9.   </startup>  
  10.   <entityFramework>  
  11.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">  
  12.       <parameters>  
  13.         <parameter value="Data Source=.\SQL2008; UID=sa;PWD=123; MultipleActiveResultSets=True" />  
  14.       </parameters>  
  15.     </defaultConnectionFactory>  
  16.   </entityFramework>  
  17. </configuration>  


另一种是通过传统的 “connectionStrings” 配置,只要配置了连接字符串“defaultConnectionFactory”将不再生效,EF会自动使用该连接。在接下来的示例中我们将采用这种方式。

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <configSections>  
  4.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  5.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  6.   </configSections>  
  7.   <connectionStrings>  
  8.     <add name="CodeFirstDb" connectionString="Data Source=.\SQL2008;Database=CodeFirstDb;UID=sa;PWD=123;MultipleActiveResultSets=true" providerName="System.Data.SqlClient"></add>  
  9.   </connectionStrings>  
  10.   <startup>  
  11.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
  12.   </startup>  
  13.   <entityFramework>  
  14.     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">  
  15.       <parameters>  
  16.         <parameter value="v11.0" />  
  17.       </parameters>  
  18.     </defaultConnectionFactory>  
  19.   </entityFramework>  
  20. </configuration>  


 

Code First数据库迁移

现在让我们在上一篇文章Entity Framework 5.0系列之EF概览中的“Code First”示例的基础上给Order添加一个”Employee”属性,然后运行,不出意外的话你将看到如下异常:

从异常信息我们可以看出,EF已经检测到模型发生了改变,建议我们使用”Code First Migrations”对模型进行更新。事实上EF就是依靠上一篇文章中提到的“dbo.__MigrationHistory”表对我们的模型进行判断的,因为这张表中存储了我们的模型结构(在Model列中),如果运行程序时你跟踪SQL Server会发现EF执行了如下SQL进行模型修改判断:

[sql] view plain copy
  1. SELECT Count(*) FROM sys.databases WHERE [name]=N'CodeFirstDb'  
  2. go  
  3. SELECT TABLE_SCHEMA SchemaName, TABLE_NAME Name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'  
  4. go  
  5. SELECT Count(*) FROM sys.databases WHERE [name]=N'CodeFirstDb'  
  6. go  
  7. SELECT   
  8. [GroupBy1].[A1] AS [C1]  
  9. FROM ( SELECT   
  10.     COUNT(1) AS [A1]  
  11.     FROM [dbo].[__MigrationHistory] AS [Extent1]  
  12. )  AS [GroupBy1]  
  13. go  
  14. SELECT TOP (1)   
  15. [Project1].[C1] AS [C1],   
  16. [Project1].[MigrationId] AS [MigrationId],   
  17. [Project1].[Model] AS [Model]  
  18. FROM ( SELECT   
  19.     [Extent1].[MigrationId] AS [MigrationId],   
  20.     [Extent1].[Model] AS [Model],   
  21.     1 AS [C1]  
  22.     FROM [dbo].[__MigrationHistory] AS [Extent1]  
  23. )  AS [Project1]  
  24. ORDER BY [Project1].[MigrationId] DESC  
  25. go  


在开始Code First数据库迁移之前,我们先对上一节编写的OrderContext类进行修改添加默认构造函数,因为Code First Migrations将会使用数据库上下文的默认构造函数进行数据迁移操作(尽管没有默认构造函数所有的数据操作都能正常进行,但是对于数据迁移这是必须的),因此我们需要添加一个默认构造函数,并且该构造函数中必须传入我们的数据库连接名称(如果使用上面我们说的第一种数据库连接配置方式则可以不用传递该参数),否则将会把更新应用到EF默认数据库上。下面是我们的OrderContext:

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data.Entity;  
  6.   
  7.   
  8. namespace CodeFirst  
  9. {  
  10.     public class OrderContext:DbContext  
  11.     {  
  12.         public OrderContext()  
  13.             : base("CodeFirstDb")  
  14.         {  
  15.         }  
  16.   
  17.         public DbSet<Order> Orders  
  18.         {  
  19.             get;  
  20.             set;  
  21.         }  
  22.   
  23.         public DbSet<OrderDetail> OrderDetails  
  24.         {  
  25.             get;  
  26.             set;  
  27.         }  
  28.     }  
  29. }  


 

下面我们将借助于”Code First Magrations” 进行模型更新。

在Visual Studio中执行:Tools->Library Package Manager->Package Manager Console来打开包管理命令行:

在命令行中输入“Enable-Migrations -StartUpProjectName CodeFirst”然后回车(“CodeFirst”是你的项目名称,如果在“Package Manager Console”中选择了默认项目可以不设置“-StartUpProjectName”参数;如果多次执行此命令可以添加-Force参数):

 

注意:如果在运行“Enable-Migrations -StartUpProjectName CodeFirst”命令过程中收到如下错误,请尝试下面给出的方法:

Enable-Migrations : The term 'Enable-Migrations' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

1. 在Package Manager Console中执行命令:Import-Module F:\[7]CSharp\EntityFramework\CodeFirst\packages\EntityFramework.5.0.0\tools\EntityFramework.PS3.psd1(后面的路径按照你EF包的实际位置修改,这个问题是由于没有导入相应命令造成的)

2. 执行“Install-Package EntityFramework -IncludePrerelease命令重新安装最新版EF(也可以通过-Version参数指定具体版本)。

3. 更新Nuget,具体更新步骤见Installing NuGet。

4. 另外,命令执行过程中其他错误可以按照错误提示解决。

如果没有错误你的项目中将自动生成一个名为”Migrations“的文件夹,里面包含两个文件: Configuration.cs和201308211510117_InitialCreate.cs(201308211510117是时间戳)。

Configuration.cs:是迁移配置代码,一般我们不需要修改。

[csharp] view plain copy
  1. namespace CodeFirst.Migrations  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity;  
  5.     using System.Data.Entity.Migrations;  
  6.     using System.Linq;  
  7.   
  8.     internal sealed class Configuration : DbMigrationsConfiguration<CodeFirst.OrderContext>  
  9.     {  
  10.         public Configuration()  
  11.         {  
  12.             AutomaticMigrationsEnabled = true;  
  13.         }  
  14.   
  15.         protected override void Seed(CodeFirst.OrderContext context)  
  16.         {  
  17.             //  This method will be called after migrating to the latest version.  
  18.   
  19.             //  You can use the DbSet<T>.AddOrUpdate() helper extension method   
  20.             //  to avoid creating duplicate seed data. E.g.  
  21.             //  
  22.             //    context.People.AddOrUpdate(  
  23.             //      p => p.FullName,  
  24.             //      new Person { FullName = "Andrew Peters" },  
  25.             //      new Person { FullName = "Brice Lambson" },  
  26.             //      new Person { FullName = "Rowan Miller" }  
  27.             //    );  
  28.             //  
  29.         }  
  30.     }  
  31. }  


201308211510117_InitialCreate.cs:以代码的形式记录了本地数据库的表结构定义。

[csharp] view plain copy
  1. namespace CodeFirst.Migrations  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity.Migrations;  
  5.       
  6.     public partial class InitialCreate : DbMigration  
  7.     {  
  8.         public override void Up()  
  9.         {  
  10.             CreateTable(  
  11.                 "dbo.Orders",  
  12.                 c => new  
  13.                     {  
  14.                         Id = c.Int(nullable: false, identity: true),  
  15.                         Customer = c.String(),  
  16.                         OrderDate = c.DateTime(nullable: false),  
  17.                     })  
  18.                 .PrimaryKey(t => t.Id);  
  19.               
  20.             CreateTable(  
  21.                 "dbo.OrderDetails",  
  22.                 c => new  
  23.                     {  
  24.                         Id = c.Int(nullable: false, identity: true),  
  25.                         Product = c.String(),  
  26.                         UnitPrice = c.String(),  
  27.                         OrderId = c.Int(nullable: false),  
  28.                     })  
  29.                 .PrimaryKey(t => t.Id)  
  30.                 .ForeignKey("dbo.Orders", t => t.OrderId, cascadeDelete: true)  
  31.                 .Index(t => t.OrderId);  
  32.               
  33.         }  
  34.           
  35.         public override void Down()  
  36.         {  
  37.             DropIndex("dbo.OrderDetails"new[] { "OrderId" });  
  38.             DropForeignKey("dbo.OrderDetails""OrderId""dbo.Orders");  
  39.             DropTable("dbo.OrderDetails");  
  40.             DropTable("dbo.Orders");  
  41.         }  
  42.     }  
  43. }  


 

现在在” Package Manager Console”中执行“Add-Migration AddEmployee”命令,该命令会自动比较模型和当前数据库中的表结构,然后执行“AddEmployee”添加一个”Employee”列,此时在”Migrations“文件夹会生成一个名为“201308240757094_AddEmployee.cs”的类(201308240757094是时间戳):

201308240757094_AddEmployee.cs内容如下:

[cpp] view plain copy
  1. namespace CodeFirst.Migrations  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity.Migrations;  
  5.       
  6.     public partial class AddEmployee : DbMigration  
  7.     {  
  8.         public override void Up()  
  9.         {  
  10.             AddColumn("dbo.Orders""Employee", c => c.String());  
  11.         }  
  12.           
  13.         public override void Down()  
  14.         {  
  15.             DropColumn("dbo.Orders""Employee");  
  16.         }  
  17.     }  
  18. }  


 

从这个类中我们可以看出事实上是给“Order”添加了“Employee”列,当然我们也可以手动修改这个类。

接下来,在“Package Manager Console”中执行“Update-Database -StartUpProjectName CodeFirst –Verbose”命令(添加-Verbose参数可以查看到更新脚本),进行数据库结构更新:

此时查看数据库,发现“Order”表已经多了一列:

OK,今天我们的内容就先到此,关于如何根据数据库生成代码类及其更多内容我们在后面的文章中再一起探讨。

0 0
原创粉丝点击