Trong chương trước, bạn đã học về di chuyển tự động, tự động cập nhật lược đồ cơ sở dữ liệu khi bạn thay đổi các lớp miền. Tại đây, bạn sẽ tìm hiểu về di chuyển dựa trên mã
Di chuyển dựa trên mã cung cấp nhiều quyền kiểm soát hơn đối với quá trình di chuyển và cho phép bạn định cấu hình những thứ bổ sung, chẳng hạn như đặt giá trị mặc định của cột, định cấu hình cột được tính, v.v.
Để sử dụng di chuyển dựa trên mã, bạn cần thực thi các lệnh sau trong Bảng điều khiển quản lý gói trong Visual Studio
- Kích hoạt di chuyển. Cho phép di chuyển trong dự án của bạn bằng cách tạo một lớp
Configuration
- Thêm di chuyển. Tạo một lớp di chuyển mới theo tên được chỉ định với các phương thức
Up[]
vàDown[]
- Cập nhật cơ sở dữ liệu. Thực thi tệp di chuyển cuối cùng được tạo bởi lệnh
Add-Migration
và áp dụng các thay đổi cho lược đồ cơ sở dữ liệu
Để sử dụng di chuyển dựa trên mã, trước tiên hãy thực thi lệnh enable-migrations
trong Bảng điều khiển quản lý gói. Mở Bảng điều khiển quản lý gói từ Công cụ → Trình quản lý gói thư viện → Bảng điều khiển quản lý gói rồi chạy lệnh enable-migrations
[đảm bảo rằng dự án mặc định là dự án có lớp ngữ cảnh của bạn]
Lệnh Enable-Migrations
sẽ tạo lớp Configuration
dẫn xuất từ
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] [] Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-IgnoreChanges] [-AppDomainBaseDirectory ] [] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".1 với
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] [] Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-IgnoreChanges] [-AppDomainBaseDirectory ] [] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".2
Bây giờ, bạn cần thiết lập trình khởi tạo cơ sở dữ liệu
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] [] Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-IgnoreChanges] [-AppDomainBaseDirectory ] [] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".3 trong lớp ngữ cảnh của mình, như được hiển thị bên dưới
public class SchoolContext: DbContext { public SchoolDBContext[]: base["SchoolDB"] { Database.SetInitializer[new MigrateDatabaseToLatestVersion[]]; } public DbSet Students { get; set; } protected override void OnModelCreating[DbModelBuilder modelBuilder] { } }
Bây giờ, bạn phải tạo một lớp di chuyển bằng cách sử dụng lệnh Add-Migration
với tên của lớp di chuyển của bạn, như hình bên dưới
Lệnh trên sẽ tạo một tệp
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] [] Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-IgnoreChanges] [-AppDomainBaseDirectory ] [] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".5 với các phương thức
Up[]
và Down[]
, như hình bên dướiNhư bạn có thể thấy, phương thức Up[]
chứa mã để tạo các đối tượng cơ sở dữ liệu và phương thức Down[]
chứa mã để loại bỏ hoặc xóa các đối tượng cơ sở dữ liệu. Bạn cũng có thể viết mã tùy chỉnh của riêng mình cho các cấu hình bổ sung. Đây là lợi thế so với di chuyển tự động
Để biết thêm về các tham số lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".0, hãy thực hiện các lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".1 hoặc
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".2 trong PMC, như hình bên dưới
PM> get-help add-migration NAME Add-Migration SYNOPSIS Scaffolds a migration script for any pending model changes. SYNTAX Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-IgnoreChanges] [-AppDomainBaseDirectory ] [] Add-Migration [-Name] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-IgnoreChanges] [-AppDomainBaseDirectory ] [] DESCRIPTION Scaffolds a new migration script and adds it to the project. RELATED LINKS REMARKS To see the examples, type: "get-help Add-Migration -examples". For more information, type: "get-help Add-Migration -detailed". For technical information, type: "get-help Add-Migration -full".
Sau khi tạo tệp di chuyển bằng lệnh add-migration, bạn phải cập nhật cơ sở dữ liệu. Thực hiện lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".3 để tạo hoặc sửa đổi lược đồ cơ sở dữ liệu. Sử dụng tùy chọn
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".4 để xem các câu lệnh SQL đang được áp dụng cho cơ sở dữ liệu đích
Thực hiện lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".5 hoặc
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".6 trong PMC để biết thêm về lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".
Tại thời điểm này, cơ sở dữ liệu sẽ được tạo hoặc cập nhật. Bây giờ, bất cứ khi nào bạn thay đổi các lớp miền, hãy thực thi Add-Migration
với tham số tên để tạo một tệp di chuyển mới và sau đó thực hiện lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".3 để áp dụng các thay đổi cho lược đồ cơ sở dữ liệu
Rollback Migration
Giả sử bạn muốn khôi phục lược đồ cơ sở dữ liệu về bất kỳ trạng thái nào trước đó, thì bạn có thể thực thi lệnh
PM> get-help update-database NAME Update-Database SYNOPSIS Applies any pending migrations to the database. SYNTAX Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] [-ConnectionStringName ] [-AppDomainBaseDirectory ] [] Update-Database [-SourceMigration ] [-TargetMigration ] [-Script] [-Force] [-ProjectName ] [-StartUpProjectName ] [-ConfigurationTypeName ] -ConnectionString -ConnectionProviderName [-AppDomainBaseDirectory ] [] DESCRIPTION Updates the database to the current model by applying pending migrations. RELATED LINKS REMARKS To see the examples, type: "get-help Update-Database -examples". For more information, type: "get-help Update-Database -detailed". For technical information, type: "get-help Update-Database -full".9 với tham số
Configuration
0 đến điểm mà bạn muốn khôi phục. Ví dụ: giả sử có nhiều lần di chuyển được áp dụng cho cơ sở dữ liệu SchoolDB ở trên nhưng bạn muốn quay lại lần di chuyển đầu tiên. Sau đó thực hiện lệnh sau