Code-First Migration

Posted caolingyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code-First Migration相关的知识,希望对你有一定的参考价值。

原文:http://www.entityframeworktutorial.net/code-first/migration-in-code-first.aspx

实体框架Code-First在EF 4.3之前具有不同的数据库初始化策略,如CreateDatabaseIfNotExists,DropCreateDatabaseIfModelChanges或DropCreateDatabaseAlways。但是,这些策略存在一些问题,例如,如果您的数据库中已经有数据(种子数据除外)或现有的存储过程,触发器等,则这些策略会用于丢弃整个数据库并重新创建它,因此您将失去数据和其他数据库对象。

实体框架4.3引入了一个迁移工具,当你的模型发生改变而不会丢失任何现有数据或其他数据库对象时,它自动更新数据库模式。它使用一个名为MigrateDatabaseToLatestVersion的新数据库初始化程序

有两种迁移:

  1. 自动迁移
  2. 基于代码的迁移

这里着重 代码迁移,有以下三个步骤:

1、 enable-migrations –EnableAutomaticMigration:$true

2、add-migration Initial

3、update-database -verbose

 

PM> enable-migrations –EnableAutomaticMigration:$true
Checking if the context targets an existing database...
Code First Migrations enabled for project EFRepositories.
PM> add-migration Initial
Scaffolding migration ‘Initial‘.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running ‘Add-Migration Initial‘ again.
PM> update-database -verbose
Using StartUp project ‘UI‘.
Using NuGet project ‘EFRepositories‘.
Specify the ‘-Verbose‘ flag to view the SQL statements being applied to the target database.
Target database is: ‘FullLibrary‘ (DataSource: localhost, Provider: System.Data.SqlClient, Origin: Configuration).
Applying explicit migrations: [201712120937570_Initial].
Applying explicit migration: 201712120937570_Initial.
CREATE TABLE [dbo].[Sys_User] (
[ID] [uniqueidentifier] NOT NULL,
[UserID] [int] NOT NULL IDENTITY,
[Name] [nvarchar](200),
[Password] [nvarchar](200),
[Salt] [nvarchar](200),
[IsLocked] [bit] NOT NULL,
[RealName] [nvarchar](200),
[Email] [nvarchar](200),
[Mobile] [nvarchar](200),
[LastLoginDate] [datetime],
[CreatedDate] [datetime],
[CreatedBy] [nvarchar](50),
[LastUpdatedDate] [datetime],
[LastUpdatedBy] [nvarchar](50),
CONSTRAINT [PK_dbo.Sys_User] PRIMARY KEY ([ID])
)
CREATE TABLE [dbo].[__MigrationHistory] (
[MigrationId] [nvarchar](150) NOT NULL,
[ContextKey] [nvarchar](300) NOT NULL,
[Model] [varbinary](max) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
)
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N‘201712120937570_Initial‘, N‘EFRepositories.Migrations.Configuration‘, 0x1F8B0800000000000400CD59DB6EDB38107D5F60FF41D0736AE58202DD406E91D87161AC734194F475414B63872845AA24958DBFAD0FFB49FB0B3BD4FD665BBEA4580408AC21E7706648CDE1B1FFFDF98FFBE52D64D62B4845051FDA678353DB02EE8B80F2E5D08EF5E2C327FBCBE7DF7F736F82F0CDFA96CFBB30F3D093ABA1FDA27574E938CA7F8190A841487D299458E8812F428704C2393F3DFDC3393B7300216CC4B22CF731E69A86903CE0E348701F221D13762B02602AB3E38897A05A77240415111F86F6CDE41122A1A8169282B2AD2B460946E1015BD816E15C68A231C6CB67059E96822FBD080D843DAD22C0790BC21464B15F96D3FBA6717A6ED2704AC71CCA8F9516E18E806717595D9CA6FB5ED5B58BBA61E56EB0C27A65B24EAA37B4B120D2B69A0B5D8E98349386F618CD940FBC95D2100ECCEC132BB59D141B8FE7C3FC9D58A398E958C29043AC256127D6433C67D4FF13564FE23BF0218F19AB868301E158CD80A6072922907AF5088B2CC8E9D8B69CBA9FD3742CDC2A3E690A5F631AD8D61DAE4DE60C8ACD7636BA9B4C4B8829D717E71D18958A7978F4E02B70904443F040B40689FB3D0D2029F9B6F5CCFF7C353CA1F8A2D9D62D799B015FEA97A18DFB695B13FA06416EC92278E614DF4B74D232DE9AD40351EA6F2183775FC8234CBFFB22533513FE7728B2B9168201E13B6FF52310F64BCA7F83AF0D7BF7556EC59CB2F74F6646949E8925E5633CEFF96AE6F3130DB73A8F2498B7E400D7EBD5860C3F1E2BC1E728D837CE8AFBB163759DB28DB79B3BD2A6C6F60C320B64826FC3786EACF0A63B5A3D76BAACDBAB6CB17A5E29AA07BAD2199161CB10529A1DA44CD215671151C9E04E4AE139D53B6BB8DEBD25518415AB707F66B1BC94F8471FBCDD59314C311C5F759063116DB1127676B284C6282E8D914EA8541A8F049913B369A3206C4DABD77F4D6DF3B5AA256EF26259F17CB6F99CB596DAFD27DD8B867F59BB09A613222D2599411143761968B925172EC288EC60D8916071C8D7B1F426EF9C60AB08B9AD3F4ADAB7AB18A9A53F42C9895594D2DA1F2925BD2A4A6AE98F50325AADAE85B53F52C96955A4D2DA1F2923AD2A4C66EA8F9153521524B7F54769304E15AC31D41FB3464455C4DAC0CE78A6E377A05DB7AE81DBF2AD115033E3DAE05EB8CD381B436D4CD769F4906693725A5DAA716F6FB6BC4D7CD19C52AC5EF046831FDCAC576F178CADE69D4EB12D2CD62B0D4CE3CE348F9930F07EB011A3986F39E19670BA00A55369636E559F1ABAF3FFA3011DA502D64308FE726D1673FA23069A88A40535178883741A351BB45589EDB844551BF05722FD1722DB17EA12741F25762CDCAAF03A16665367CDA93E58631D2BB69AA43A16685D411D0BB553309956AB1335B11B5887803A10AA14299D097FDC2BDF0EFDB46F989D7A6AAF5077934FED5BFE7679B4561DA5248345980B4C210D1069E6AF83B4539BF35CA7FA55AA3B0645972584F96295836FC8A404CDE74CF942E495C7A4AA11E5539A6F0B6882DB42AE24B670E26B1CF641A944E77E232C4EDED23904537E1FEB28D6574A413867B5EB85EB6C5E3F1188F598DDFBC83CA963A480615273B2EEF9754C5950C43D69B7B67510E698649C8351A1CE47B8E5AA40BA13BC275056BE3144C00D633D4118310453F7DC23AFB04F6C78BA66B024FE2ABFBAAC07D9BE11F5B2BB634A9692842AC328FDCDCF038EF97DE0F37FE2546CA451180000 , N‘6.1.0-30225‘)

Running Seed method.

以上是关于Code-First Migration的主要内容,如果未能解决你的问题,请参考以下文章

EntityFramework Code-First 简易教程-------领域类的配置

EF Code-First 学习之旅

EFEF Code-First数据迁移

Code-First简介

7.DataAnnotations(数据注解)Code-First 系列

Code-First 迁移问题