V
V
Viktor Familyevich2020-07-03 09:50:51
ASP.NET
Viktor Familyevich, 2020-07-03 09:50:51

How to apply migration correctly?

Good afternoon. Is there a database with the dbo.Services table in which one more isPopular column needs to be added, has the public virtual bool field been registered in the object? isPopular {get; set; } further added migration
dotnet-ef migrations add _ispopular
but after updating the database an error appears


PS C:\Users\38418\source\repos\Wintego\siteNetCore31\siteNetCore31> dotnet-ef database update _ispopular
Build started...
Build succeeded.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (111ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
[Id] nvarchar(450) NOT NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
Failed executing DbCommand (111ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
[Id] nvarchar(450) NOT NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ( [Id])
);

Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'AspNetRoles' in the database.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning at (TdsParserStateObject stateObj, callerHasConnectionLock Boolean, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun (RunBehavior runBehavior, cmdHandler SqlCommand, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & dataReady)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean isAsync, Int32 timeout, Boolean asyncWrite)
at Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletion`1 completion, BooleanSource sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String methodName)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery migrationCommandable`1 , IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase. Execute(Action action)
ClientConnectionId:2c49b93b-9409-4ed7-bdcd-dffd648e4e40
Error Number:2714,State:6,Class:16
There is already an object named 'AspNetRoles' in the database.

Question - how to add a field without touching other tables?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
yuopi, 2020-07-03
@yuopi

Did you accidentally delete the classes of old migrations?
and why virtual property?
If the old migrations are in place, then you can fix the new migration by removing the AspNetRoles table creation code from it.

R
Roman, 2020-07-04
@yarosroman

Most likely, the service table for migrations was deleted, ef stores a snapshot of your dbcontext in it, and uses it during migration to understand which migration was applied. EFMigrationHistory is called.
As a recipe, we set up a connection to another db. Delete the last migration. Doing a database update. We transfer the table to the old database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question