Code First 下自动更新数据库结构(Automatic Migrations)

本文介绍如何使用Entity Framework 4.3进行数据库自动迁移,包括配置Web.config、创建实体模型及上下文,并通过NuGet命令实现迁移功能。

示例 Web.config

复制代码
<?xml version="1.0" encoding="utf-8"?>
<configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <connectionStrings> <!-- 需要将 Persist Security Info 设置为 True,以便保存密码信息 因为 Database.SetInitializer<MyContext>(new DropCreateDatabaseIfModelChanges<MyContext>()); 在判断 Code First 与数据库结构是否一致时需要连接 master 库 --> <add name="MyConnection" providerName="System.Data.SqlClient" connectionString="server=.;database=MyDB;uid=sa;pwd=111111;Persist Security Info=True" /> </connectionStrings> </configuration>
复制代码

Product.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace EF43.UpdateSchema { [Table("Product")] public class Product { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ProductId { get; set; } [Required] [Column("ProductName", TypeName="varchar")] public string Name { get; set; } /* 为了测试 Automatic Migrations 可放开此注释,然后 Add-Migration 的话会自动生成结构迁移的相关代码 public double Price { get; set; } */ } }
复制代码

MyContext.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq; using System.Web; using System.Data.Entity; namespace EF43.UpdateSchema { // 创建的 Context 要继承自 DbContext public class MyContext : DbContext { // 指定数据库连接为:connectionStrings 中的 MyConnection public MyContext() : base("MyConnection") { } public DbSet<Product> Products { get; set; } } }
复制代码

Demo.aspx.cs

复制代码
/*
 * 通过 NuGet 下载 Entity Framework 4.3
 * 
 * 关于 Code First 下自动更新数据库结构(Automatic Migrations)的相关说明如下:
 * 注:需要通过 NuGet 的 Package Manager Console 输入相关命令
 * 
 * 在更新了实体结构后输入如下命令
 * 1、Enable-Migrations
 *     启动迁移功能,会在项目根目录下生成 Migrations 文件夹,其内通常会有两个文件
 *     1. Configuration.cs - 相关配置,如是否需要自动迁移(默认为 false)等
 *     2. 201202290715581_InitialCreate.cs - 未迁移前的数据结构,前半部分为时间戳
 *     
 * 2、Add-Migration -StartupProjectName EF43
 *     在指定的项目中增加一个迁移点,此命令后会要求输入一个 Name 参数,此参数的值为迁移点名称
 *     假设输入的迁移点名称为 MyFirstTest 则会生成一个类似如下的文件 201202290718442_MyTestFirst.cs,其包含两个方法 Up() 和 Down(),分别用于此迁移点的升级和降级
 *     
 * 3、Update-Database -StartupProjectName EF43(将指定的项目的数据库结构升级到最新)
 *     Update-Database -TargetMigration:"201202290718442_MyTestFirst",将当前数据库结构升级到此迁移点(无参数 -TargetMigration 则为升级到最新)
 *     Update-Database -Script,显示用于更新数据库结构的相关 sql 代码
 *     Update-Database -Script -SourceMigration:"aaa" -TargetMigration:"bbb",显示将迁移点“aaa”升/降级到迁移点“bbb”的相关 sql 代码
 *     
 * 注:如果发生“System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation”异常,请查看是否显示指定了项目名(本例为 -StartupProjectName EF43)
 */

using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.Entity; namespace EF43.UpdateSchema { public partial class Demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { using (var db = new MyContext()) { Random random = new Random(); var product = new Product { Name = "windows " + random.Next() }; db.Products.Add(product); int recordsAffected = db.SaveChanges(); Response.Write("影响到数据库的行数:" + recordsAffected.ToString()); } } } }
复制代码

Migrations/Configuration.cs

复制代码
namespace EF43.Migrations
{
    using System;
    using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration<EF43.UpdateSchema.MyContext> { public Configuration() { // 默认情况下不会自动迁移数据库结构,还有许多其它相关设置,详看 DbMigrationsConfiguration<TContext> AutomaticMigrationsEnabled = false; } protected override void Seed(EF43.UpdateSchema.MyContext 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" } // ); //  } } }
复制代码

Migrations/201202290715581_InitialCreate.cs

复制代码
namespace EF43.Migrations
{
    using System.Data.Entity.Migrations;
    
    public partial class InitialCreate : DbMigration { public override void Up() { CreateTable( "Product", c => new { ProductId = c.Int(nullable: false, identity: true), ProductName = c.String(nullable: false, unicode: false), }) .PrimaryKey(t => t.ProductId); } public override void Down() { DropTable("Product"); } } }
复制代码

Migrations/201202290718442_MyTestFirst.cs

复制代码
namespace EF43.Migrations
{
    using System.Data.Entity.Migrations;
    
    public partial class MyTestFirst : DbMigration { public override void Up() { // AddColumn("Product", "Price", c => c.Double(nullable: false)); // 自动生成的代码如上,下面是我自定义的,用于设置新增字段的默认值 // 还有许多其它相关设置,详看 DbMigration AddColumn("Product", "Price", c => c.Double(nullable: false, defaultValue: 10d)); } public override void Down() { DropColumn("Product", "Price"); } } }
复制代码

转载于:https://www.cnblogs.com/zxh1919/p/7865891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值