C# EF Core无法将数据从旧的SQL Server数据库传输到新的SQL Server数据库

C# EF Core无法将数据从旧的SQL Server数据库传输到新的SQL Server数据库,c#,sql-server,asp.net-core,asp.net-core-mvc,entity-framework-core,C#,Sql Server,Asp.net Core,Asp.net Core Mvc,Entity Framework Core,复制步骤 我有一个ASP.NETCore2.2Web应用程序,其后端SQLServerDB是在本教程的EntityFrameworkCore中创建的 总结这些步骤 在链接中下载项目 转到appsettings.json 将连接字符串中的数据库名称更改为contosuniversity2 保存更改并生成项目 确保没有程序实例正在运行,并使用CLI(命令行界面)或PMC(包管理控制台),导航到项目文件夹并输入此命令dotnet ef迁移添加InitialCreate 现在输入命令dotnet ef数

复制步骤

我有一个ASP.NETCore2.2Web应用程序,其后端SQLServerDB是在本教程的EntityFrameworkCore中创建的

总结这些步骤

  • 在链接中下载项目

  • 转到
    appsettings.json

  • 将连接字符串中的数据库名称更改为
    contosuniversity2

  • 保存更改并生成项目

  • 确保没有程序实例正在运行,并使用CLI(命令行界面)或PMC(包管理控制台),导航到项目文件夹并输入此命令
    dotnet ef迁移添加InitialCreate

  • 现在输入命令
    dotnet ef数据库更新

  • 预期行为

    实体框架将数据从以前名为ContosoUniversity1的数据库迁移到ContosoUniversity2,包括所有列和表。它在Visual Studio 2019中为作者工作

    实际行为

    我只看到所有表和列的创建。除了在迁移历史记录中只有一行的_EFMigrationsHistory中,没有其他数据。我从CLI获取此输出

    `Build started...
    Build succeeded.
    info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
          Entity Framework Core 2.2.6-servicing-10079 initialized 'SchoolContext' us
    ing provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (666ms) [Parameters=[], CommandType='Text', CommandTime
    out='60']
          CREATE DATABASE [ContosoUniversity2];
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (161ms) [Parameters=[], CommandType='Text', CommandTime
    out='60']
          IF SERVERPROPERTY('EngineEdition') <> 5
          BEGIN
              ALTER DATABASE [ContosoUniversity2] SET READ_COMMITTED_SNAPSHOT ON;
          END;
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeo
    ut='30']
          CREATE TABLE [__EFMigrationsHistory] (
              [MigrationId] nvarchar(150) NOT NULL,
              [ProductVersion] nvarchar(32) NOT NULL,
              CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
          );
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          SELECT [MigrationId], [ProductVersion]
          FROM [__EFMigrationsHistory]
          ORDER BY [MigrationId];
    info: Microsoft.EntityFrameworkCore.Migrations[20402]
          Applying migration '20191227004521_InitialCreate'.
    Applying migration '20191227004521_InitialCreate'.
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          CREATE TABLE [Course] (
              [CourseID] int NOT NULL,
              [Title] nvarchar(max) NULL,
              [Credits] int NOT NULL,
              CONSTRAINT [PK_Course] PRIMARY KEY ([CourseID])
          );
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          CREATE TABLE [Student] (
              [ID] int NOT NULL IDENTITY,
              [LastName] nvarchar(max) NULL,
              [FirstMidName] nvarchar(max) NULL,
              [EnrollmentDate] datetime2 NOT NULL,
              CONSTRAINT [PK_Student] PRIMARY KEY ([ID])
          );
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          CREATE TABLE [Enrollment] (
              [EnrollmentID] int NOT NULL IDENTITY,
              [CourseID] int NOT NULL,
              [StudentID] int NOT NULL,
              [Grade] int NULL,
              CONSTRAINT [PK_Enrollment] PRIMARY KEY ([EnrollmentID]),
              CONSTRAINT [FK_Enrollment_Course_CourseID] FOREIGN KEY ([CourseID]) RE
    FERENCES [Course] ([CourseID]) ON DELETE CASCADE,
              CONSTRAINT [FK_Enrollment_Student_StudentID] FOREIGN KEY ([StudentID])
     REFERENCES [Student] ([ID]) ON DELETE CASCADE
          );
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          CREATE INDEX [IX_Enrollment_CourseID] ON [Enrollment] ([CourseID]);
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          CREATE INDEX [IX_Enrollment_StudentID] ON [Enrollment] ([StudentID]);
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeou
    t='30']
          INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
          VALUES (N'20191227004521_InitialCreate', N'2.2.6-servicing-10079');
    Done.`
    

    迁移没有什么问题。解决方案是恢复到备份文件夹中的上一个备份,复制并粘贴教程中的代码,而不是手工编写。事实证明,我只拼错了一行,并且能够纠正错误。

    您是否希望所有表中的数据都被复制到另一个表中?是@BrendanGreen,因为在MS教程的图像中显示数据被复制到另一个表中。我想你误读了这里的一个词。迁移用于将同一数据库中的数据从一个版本(表结构)迁移到另一个版本。@vasily sib,所以它应该像我想的那样跨数据库复制数据。但是我已经读了一遍又一遍的步骤。我想知道我可能错过了什么。@JordanNash“迁移”不复制数据。它们只是对数据库模式进行更改。在这种情况下,您已经有效地设置了一个新的连接字符串。我假设“新”数据库还不存在,所以迁移应用了创建新数据库的命令,并创建模式(表、索引、视图等),以便与应用程序中EF上下文中配置的内容相匹配。如果数据库中已经存在数据,则如果可能,这些命令将保留这些数据。
    .NET Core SDK (reflecting any global.json):
     Version:   2.2
    
    Runtime Environment:
     OS Name:     Windows
     OS Version:  6.1.7601
     OS Platform: Windows
     RID:         win7-x64