這個系列自開始寫的時候終端很久了,沒辦法這段時間實在是太忙,無暇顧及啊。不過我在這裡開始就給大家一個好的回覆。希望以後時間能多點。 從系列三開始我開始講Code first add-migration 大家知道之前的model first雖然設計領域比較直觀和明了。但是有一個很大的缺陷,每次設計好領域後都需要重新去產生資料庫結構,然後導致資料的丟失,這個痛苦我想大家在用Model first 的時候都深有體會,那麼我們慶幸我們有Code First 中的資料移轉足夠去為我們解決這些事問題了,讓我們即使改變領域但是我們依舊可以保持我們的配置資料(例如菜單配置)。
首先我們開啟vs建立一個Egojit.Domain層,下一步就很重要了,我們開啟Nuget
在控制台輸入install-package entityframework 去安裝entityframework 預設安裝的是5.0如果出現3.說明你安裝成功!它會給你自動載入entityframework庫.並且自動為項目添加packages.config配置包,代碼如下:
<?xml version="1.0" encoding="utf-8"?><packages> <package id="EntityFramework" version="5.0.0" targetFramework="net40" /></packages>
這樣我們就為我們的項目加上了EF。而不需要手動去添加。
然後為我們的項目添加一個User類,我們這裡只是為了測試EF code first add-migration.User代碼如下:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Text;namespace Egojit.Domain{ public class User { [Key] public Guid Id { get; set; } public string Name { get; set; } public string Pwd { get; set; } }}
比較簡單的代碼。但足夠我們去理解。
然後我們再為我們的項目添加Context類 EgojitFrameworkContext。代碼如下:
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;namespace Egojit.Domain{ public class EgojitFrameworkContext:DbContext { public DbSet<User> Users { get; set; } }}
這個EgojitFrameworkContext類需要繼承自DbContext,它就像一個容器去管理我們的領域各個類。
第四步我們需要設定資料庫,否則我們不知道資料庫產生到哪App.config檔案的配置如下:
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="EgojitFrameworkContext" connectionString="Data Source=.;Database=EgojitFramework;user Id=sa;pwd=2011623;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> </entityFramework></configuration>
connectionStrings配置節很重要。這樣我們就可以去產生資料庫了,
第五步需要在Nuget控制台中執行 enable-migrations命令,在第一次的時候需要執行這個命令,以後修改了領域就不需要了
這樣會在項目中產生一個Migrations檔案夾,並在這個檔案夾中產生類Configuration。代碼如下:
namespace Egojit.Domain.Migrations{ using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : DbMigrationsConfiguration<Egojit.Domain.EgojitFrameworkContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(Egojit.Domain.EgojitFrameworkContext 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" } // ); // } }}
第六步我們執行add-migration db命令
這樣會產生領域結構變化的代碼:這個代碼類名稱是根據時間命名和我們執行add-migration命令時加的db去組合產生的。代碼是:
namespace Egojit.Domain.Migrations{ using System; using System.Data.Entity.Migrations; public partial class db : DbMigration { public override void Up() { CreateTable( "dbo.Users", c => new { Id = c.Guid(nullable: false), Name = c.String(), Pwd = c.String(), }) .PrimaryKey(t => t.Id); } public override void Down() { DropTable("dbo.Users"); } }}
我們從代碼中我們就可以看到我們的領域被改變了,而且被改變了什麼。
最後一步我們需要在Nuget中執行update-database 命令將改變提交到我們配置的資料庫。
這樣我們就在資料庫中神奇的產生了EgojitFramework資料庫為什麼叫這個名字,大家領悟下App.config設定檔就行了。同時也產生了Users表.
當然表中沒有資料,我們現在手動添加一條記錄:
這樣我們測試當我們修改了領域的時候是否資料會被清除。我們為User類添加一個Age欄位。代碼如下:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Text;namespace Egojit.Domain{ public class User { [Key] public Guid Id { get; set; } public string Name { get; set; } public string Pwd { get; set; } public int? age { get; set; } }}
執行add-migration gg,然後update-database提交到資料庫。add-migration gg產生的程式碼如下:
namespace Egojit.Domain.Migrations{ using System; using System.Data.Entity.Migrations; public partial class gg : DbMigration { public override void Up() { AddColumn("dbo.Users", "age", c => c.Int()); } public override void Down() { DropColumn("dbo.Users", "age"); } }}
可以容易看出我們添加了age欄位,我們重新整理資料庫去查看發現之前的資料保留了:
好了就到這裡。add-migraion大功告成,以後修改領域就不用怕了,我們只需要兩條命令就OK了。
著作權聲明:出自梔子網http://www.zhizinet.com