程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> EF Code First Migrations數據庫遷移,efmigrations

EF Code First Migrations數據庫遷移,efmigrations

編輯:關於.NET

EF Code First Migrations數據庫遷移,efmigrations


1、EF Code First創建數據庫

步驟1:新建控制台應用程序

步驟2:安裝EntityFramework

在程序包管理器控制台中執行以下語句:

PM>Install-Package EntityFramework

2、項目結構

兩個實體及映射,PortalContext代碼如下:

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Enities;
using Portal.Mapping;

namespace Portal
{
    public class PortalContext : DbContext
    {
        static PortalContext()
        {
            System.Data.Entity.Database.SetInitializer<PortalContext>(null);

           //Database.SetInitializer(new DropCreateDatabaseAlways<PortalContext>());
        }

        public DbSet<Category> Categories { get; set; }
        public DbSet<Province> Provinces { get; set; }


        /// <summary>
        /// This method is called when the model for a derived context has been initialized, but
        /// before the model has been locked down and used to initialize the context.  The default
        /// implementation of this method does nothing, but it can be overridden in a derived class
        /// such that the model can be further configured before it is locked down.
        /// </summary>
        /// <remarks>
        /// Typically, this method is called only once when the first instance of a derived context
        /// is created.  The model for that context is then cached and is for all further instances of
        /// the context in the app domain.  This caching can be disabled by setting the ModelCaching
        /// property on the given ModelBuidler, but note that this can seriously degrade performance.
        /// More control over caching is provided through use of the DbModelBuilder and DbContextFactory
        /// classes directly.
        /// </remarks>
        /// <param name="modelBuilder"> The builder that defines the model for the context being created. </param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CategoryMap());
            modelBuilder.Configurations.Add(new ProvinceMap());
        }

    }
}

其他代碼,可以從本博文最後下載。

3、EF Code First數據庫遷移

步驟1:生成數據庫

修改PortailContext.cs的靜態構造函數,取消當數據庫模型發生改變時刪除當前數據庫重建數據庫的設置。

static PortalContext()
        {
            System.Data.Entity.Database.SetInitializer<PortalContext>(null);

            //Database.SetInitializer(new DropCreateDatabaseAlways<PortalContext>());
        }

步驟2:在程序包管理器控制台,執行以下語句

PM>Enable-Migrations –EnableAutomaticMigrations

運行結果如下:

Configuration代碼如下

namespace Portal.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<Portal.PortalContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(Portal.PortalContext 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" }
            //    );
            //
        }
    }
}

步驟3:在程序包管理器控制台執行以下語句

PM>Add-Migration InitialCreate

運行結果如下:

步驟4:在程序包管理器控制台執行以下語句

PM>Update-Database –Verbose

執行結果如下:

到此,數據完成了創建:

4、EF Code First添加實體

步驟1:增加City.cs實體

步驟2:在程序包管理器控制台執行以下語句:

PM>Add-Migration AddCity

執行結果如下:

步驟3:更新實體到數據庫,在程序包管理器控制台執行以下語句

PM>Update-Database –Verbose

數據庫變化如下:

5、EF Code First修改實體

步驟1:修改City,增加CityNo屬性

步驟2:在程序包管理器控制台中執行以下代碼:

PM>Add-Migration UpdateCity

執行結果如下:

步驟3:在程序包管理器控制台執行以下語句

PM>Update-Database –Verbose

執行結果如下:

6、版本回溯

步驟1:修改數據庫中的City,刪除CityNo字段

步驟2:在程序包管理器控制器執行以下語句:

PM>Add-Migration ModifyCity

步驟3:更新到數據庫,在程序包管理器控制台執行以下語句:

PM>Update-Database –Verbose

在程序包管理器控制台執行以下語句:

PM>Update-Database –SourceMigration “201701091349455_AddCity.cs”

執行結果如下:

7、其他命令

1、為指定的DbContext啟用數據庫遷移

PM> Enable-Migrations -ContextTypeName Portal.PortalContext
2、設置是否允許自動遷移
Enable-Migrations

生成的Configuration.cs類文件的構造函數

public Configuration()
{
      AutomaticMigrationsEnabled = false;
}

3、Enable-Migrations指定項目名稱

PM>Enable-Migrations -StartUpProjectName Portal

如果在“Package Manager Console”中選擇了默認項目可以不設置“-StartUpProjectName”參數;如果多次執行此命令可以添加-Force參數。

4、查看所執行的Sql語句 -Verbose指令

Update-Database -Verbose 

8、示例下載

Portal示例下載:http://files.cnblogs.com/files/zsy/Portal.rar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved