D
D
DenisVladimirovich2019-07-24 15:00:14
ASP.NET
DenisVladimirovich, 2019-07-24 15:00:14

How to do an EFCore migration update?

Good evening gentlemen. Tell me please. I'm doing a small project and ran into a problem. After I successfully did Update-Migration for the first time and tables were created in the database, I tried to simply add a field to the model and update the database. EFCore constantly issues that the table already exists. But there are changes. It is obtained instead of alter table executing create table. What could be my mistake? Unfortunately I can't post the code.

spoiler
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Product] (
[ProductId] int NOT NULL IDENTITY,
[ProductTitle] nvarchar(max) NULL,
[ProductDescription] nvarchar(max) NULL,
[Category] nvarchar(max) NULL,
[PrductPrice] int NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY ([ProductId])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Users] (
[UserId] int NOT NULL IDENTITY,
[Login] nvarchar(max) NULL,
[Name] nvarchar(max) NULL,
[LastName] nvarchar(max) NULL,
[Email] nvarchar(max) NULL,
[Password] nvarchar(max) NULL,
[Phone] nvarchar(max) NULL,
[RegID] int NOT NULL,
[UserRole] int NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY ([UserId])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Department] (
[DepartmentID] int NOT NULL IDENTITY,
[DepartmentTitle] nvarchar(max) NULL,
[UserForeignKey] int NULL,
CONSTRAINT [PK_Department] PRIMARY KEY ([DepartmentID]),
CONSTRAINT [FK_Department_Users_UserForeignKey] FOREIGN KEY ([UserForeignKey]) REFERENCES [Users] ([UserId]) ON DELETE NO ACTION
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (6ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Post] (
[PostID] int NOT NULL IDENTITY,
[PostTitle] nvarchar(max) NULL,
[PostImage] nvarchar(max) NULL,
[PostDescription] nvarchar(max) NULL,
[PostText] nvarchar(max) NULL,
[PostDS] datetime2 NOT NULL,
[PostKeyWords] nvarchar(max) NULL,
[PostMetaDesc] nvarchar(max) NULL,
[PostedByUserId] int NULL,
CONSTRAINT [PK_Post] PRIMARY KEY ([PostID]),
CONSTRAINT [FK_Post_Users_PostedByUserId] FOREIGN KEY ([PostedByUserId]) REFERENCES [Users] ([UserId]) ON DELETE NO ACTION
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Deal] (
[DealID] int NOT NULL IDENTITY,
[ParentDealID] int NOT NULL,
[ChildNumber] int NOT NULL,
[DealDS] datetime2 NOT NULL,
[DepartmentForeignKey] int NULL,
[UserForeignKey] int NULL,
[ProductForeignKey] int NULL,
CONSTRAINT [PK_Deal] PRIMARY KEY ([DealID]),
CONSTRAINT [FK_Deal_Department_DepartmentForeignKey] FOREIGN KEY ([DepartmentForeignKey]) REFERENCES [Department] ([DepartmentID]) ON DELETE NO ACTION,
CONSTRAINT [FK_Deal_Product_ProductForeignKey] FOREIGN KEY ([ProductForeignKey]) REFERENCES [Product] ([ProductId]) ON DELETE NO ACTION,
CONSTRAINT [FK_Deal_Users_UserForeignKey] FOREIGN KEY ([UserForeignKey]) REFERENCES [Users] ([UserId]) ON DELETE NO ACTION
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE INDEX [IX_Deal_DepartmentForeignKey] ON [Deal] ([DepartmentForeignKey]);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE INDEX [IX_Deal_ProductForeignKey] ON [Deal] ([ProductForeignKey]);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE INDEX [IX_Deal_UserForeignKey] ON [Deal] ([UserForeignKey]);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE UNIQUE INDEX [IX_Department_UserForeignKey] ON [Department] ([UserForeignKey]) WHERE [UserForeignKey] IS NOT NULL;
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE INDEX [IX_Post_PostedByUserId] ON [Post] ([PostedByUserId]);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Microsoft.EntityFrameworkCore.Migrations[20402]
Applying migration '20190724114851_InitialCreate'.
Applying migration '20190724114851_InitialCreate'.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [Product] (
[ProductId] int NOT NULL IDENTITY,
[ProductTitle] nvarchar(max) NULL,
[ProductDescription] nvarchar(max) NULL,
[Category] nvarchar(max) NULL,
[PrductPrice] int NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY ([ProductId])
);
В базе данных уже существует объект с именем "Product".

Here I noticed, or maybe I didn’t notice correctly, but the Product table is created twice. At the beginning and at the end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2019-07-24
@DenisVladimirovich

With each change of models and/or their connections, you must first generate the next migration ( Add-Migration), and then roll the changes onto the schema ( Update-Database).
The CLI would be like this:

dotnet ef migrations add NewMigrationName;
dotnet ef database update;

Read more here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question