EF Core Fails to transfer data into new SQL Server DB from old one

asp.net-core asp.net-core-mvc c# entity-framework-core sql-server

Question

Steps to reproduce

I have an ASP.Net Core 2.2 web application with the backend SQL Server DB made in Entity Framework Core from this tutorial. https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations?view=aspnetcore-2.2

To summarise the steps.

  1. Download the project in the link.

  2. Go to appsettings.json.

  3. Change database name in connection string to ContosoUniversity2.

  4. Save your changes and build the project.

  5. Ensure no instance of the program is running and use CLI (Command Line Interface) or PMC (Package Management Console) and navigate to the project folder and enter this command. dotnet ef migrations add InitialCreate.

  6. Now enter commanddotnet ef database update.

Expected behaviour

Entity Framework migrates data from previous database named ContosoUniversity1 to ContosoUniversity2 including all the columns and tables. It has worked for the author in Visual Studio 2019.

Actual behaviour

I am only seeing the creation of all tables and columns. There is no data other than in __EFMigrationsHistory which has a single row for the migration history. I get this output from 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.`

Environment data:

.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
1
0
12/27/2019 1:56:03 AM

Popular Answer

There was nothing wrong with the migrations. The solution was to revert to a previous backup in my backup folder and copy and paste the code from the tutorial instead of hand writing it. Turns out I only had one line misspelled and was able correct that.

0
2/6/2020 12:22:26 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow