学习补充008-xx-01 Migrations Overview(迁移概述)

Migrations Overview(迁移概述)

In real world projects, data models change as features get implemented: new entities or properties are added and removed, and database schemas need to be changed accordingly to be kept in sync with the application. The migrations feature in EF Core provides a way to incrementally update the database schema to keep it in sync with the application’s data model while preserving existing data in the database.
在现实世界的项目中,数据模型随着功能的实现而变化:添加和删除新的实体或属性,并且需要相应地更改数据库模式以与应用程序保持同步。EF Core中的迁移功能提供了一种增量更新数据库模式以使其与应用程序的数据模型保持同步的方法,同时保留数据库中的存量数据。

At a high level, migrations function in the following way:
在高层次上,迁移以以下方式发挥作用:

When a data model change is introduced, the developer uses EF Core tools to add a corresponding migration describing the updates necessary to keep the database schema in sync. EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project’s source control like any other source file.
当引入数据模型更改时,开发人员使用EF Core工具添加相应的迁移,描述保持数据库模式同步所需的更新。EF Core将当前模型与旧模型的快照进行比较以确定差异,并生成迁移源文件;这些文件可以像任何其他源文件一样在项目的源代码控制中进行跟踪。

Once a new migration has been generated, it can be applied to a database in various ways. EF Core records all applied migrations in a special history table, allowing it to know which migrations have been applied and which haven’t.
The rest of this page is a step-by-step beginner’s guide for using migrations. Consult the other pages in this section for more in-depth information.

一旦生成了新的迁移,就可以通过各种方式将其应用到数据库中。EF Core将所有应用的迁移记录在一个特殊的历史表中,使其能够知道哪些迁移已经应用,哪些没有应用。本页的其余部分是使用迁移的新手引导。有关更深入的信息,请参阅本节中的其他页面。

Getting started

Let’s assume you’ve just completed your first EF Core application, which contains the following simple model:
假设您刚刚完成了第一个EF Core应用程序,其中包含以下简单模型:

C#

public class Blog { 
    public int Id { get; set; } 
    public string Name { get; set; } 
}

During development, you may have used the Create and Drop APIs to iterate quickly, changing your model as needed; but now that your application is going to production, you need a way to safely evolve the schema without dropping the entire database.
在开发过程中,您可能已经使用创建和删除API来快速迭代,根据需要更改模型;但是现在您的应用程序即将投入生产,您需要一种方法来安全地发展模式,而不会删除整个数据库。

Install the tools(安装工具)

First, you’ll have to install the EF Core command-line tools:
首先,您必须安装EF Core命令行工具:

  • We generally recommend using the .NET Core CLI tools, which work on all platforms.
  • 我们通常建议使用。NET Core CLI工具,适用于所有平台。
  • If you’re more comfortable working inside Visual Studio or have experience with EF6 migrations, you can also use the Package Manager Console tools.
  • 如果您更喜欢在Visual Studio中工作或有EF6迁移经验,您还可以使用包管理器控制台工具。

Create your first migration(创建您的第一次迁移)

You’re now ready to add your first migration! Instruct EF Core to create a migration named InitialCreate:
您现在已准备好添加您的第一次迁移!指示EF Core创建一个名为OrialCreate的迁移:

.NET Core CLI

dotnet ef migrations add InitialCreate

Visual Studio

Add-Migration InitialCreate

EF Core will create a directory called Migrations in your project, and generate some files. It’s a good idea to inspect what exactly EF Core generated - and possibly amend it - but we’ll skip over that for now.
EF Core将在您的项目中创建一个名为Migrations的目录,并生成一些文件。检查EF Core究竟生成了什么是个好主意-并可能对其进行修改-但我们现在将跳过它。

Create your database and schema(创建您的数据库和模式)

At this point you can have EF create your database and create your schema from the migration. This can be done via the following:
此时,您可以让EF创建您的数据库并从迁移中创建您的架构。这可以通过以下方式完成:

.NET Core CLI

dotnet ef database update

Visual Studio

Update-Database

That’s all there is to it - your application is ready to run on your new database, and you didn’t need to write a single line of SQL. Note that this way of applying migrations is ideal for local development, but is less suitable for production environments - see the Applying Migrations page for more info.
这就是它的全部内容——您的应用程序已经准备好在您的新数据库上运行,您不需要编写一行SQL。请注意,这种应用迁移的方式非常适合本地开发,但不太适合生产环境——请参阅应用迁移页面了解更多信息。

Evolving your model(发展您的模型)

A few days have passed, and you’re asked to add a creation timestamp to your blogs. You’ve done the necessary changes to your application, and your model now looks like this:
几天过去了,系统要求您向博客添加创建时间戳。您已经对应用程序进行了必要的更改,您的模型现在如下所示:

C#

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime CreatedTimestamp { get; set; }
}

Your model and your production database are now out of sync - we must add a new column to your database schema. Let’s create a new migration for this:
您的模型和生产数据库现在不同步-我们必须在您的数据库模式中添加一个新列。让我们为此创建一个新的迁移:

.NET Core CLI

dotnet ef migrations add AddBlogCreatedTimestamp

Visual Studio

Add-Migration AddBlogCreatedTimestamp

Note that we give migrations a descriptive name, to make it easier to understand the project history later.
请注意,我们为迁移提供了一个描述性名称,以便以后更容易理解项目历史。

Since this isn’t the project’s first migration, EF Core now compares your updated model against a snapshot of the old model, before the column was added; the model snapshot is one of the files generated by EF Core when you add a migration, and is checked into source control. Based on that comparison, EF Core detects that a column has been added, and adds the appropriate migration.
由于这不是项目的第一次迁移,EF Core现在会在添加列之前将您更新的模型与旧模型的快照进行比较;模型快照是添加迁移时EF Core生成的文件之一,并被签入源代码控制。基于该比较,EF Core检测到已添加列,并添加适当的迁移。

You can now apply your migration as before:
您现在可以像以前一样应用迁移:

.NET Core CLI

dotnet ef database update

Visual Studio

Update-Database

Excluding parts of your model(不包括模型的一部分)

Sometimes you may want to reference types from another DbContext. This can lead to migration conflicts. To prevent this, exclude the type from the migrations of one of the DbContexts.
有时您可能希望从另一个DbContext引用类型。这可能会导致迁移冲突。为防止这种情况,请从其中一个DbContext的迁移中排除该类型。

C#

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUser>()
        .ToTable("AspNetUsers", t => t.ExcludeFromMigrations());
}

引自:
https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli

相关推荐

  1. 学习补充008-xx-01 Migrations Overview(迁移概述

    2024-07-19 11:42:06       20 阅读
  2. SiteServer 学习笔记 Day08 内容补充

    2024-07-19 11:42:06       31 阅读
  3. 001:自动驾驶概述

    2024-07-19 11:42:06       34 阅读
  4. 数据库迁移平台构思001

    2024-07-19 11:42:06       27 阅读
  5. 迁移学习的简要概述

    2024-07-19 11:42:06       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 11:42:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 11:42:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 11:42:06       58 阅读
  4. Python语言-面向对象

    2024-07-19 11:42:06       69 阅读

热门阅读

  1. 最长上升子序列模板(LIS)

    2024-07-19 11:42:06       22 阅读
  2. Apache-BeanUtils VS SpringBean-Utils

    2024-07-19 11:42:06       16 阅读
  3. MySQL中为什么不推荐使用 text 类型?

    2024-07-19 11:42:06       18 阅读
  4. 华为云认证

    2024-07-19 11:42:06       19 阅读
  5. TF和TF-IDF区别和联系

    2024-07-19 11:42:06       19 阅读