EF Core migration fails, table already created

2

Here is my DbContext class

public class BlogContext : DbContext
{
    public BlogContext (DbContextOptions<BlogContext> options)
        : base(options)
    { }
    public DbSet<User> Users { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<Comment> Comments  { get; set; }
}

And here are the commands i enter into the nugget console untill the error comes up

PM> add-migration AddNotNull
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.3-rtm-32065'. Update the tools for the latest features and bug fixes. Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 2.1.3-rtm-32065 initialized 'BlogContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None To undo this action, use Remove-Migration.
PM> update-database
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.3-rtm-32065'. Update the tools for the latest features and bug fixes. Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 2.1.3-rtm-32065 initialized 'BlogContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None 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 (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'[__EFMigrationsHistory]'); Applying migration '20181113132251_AddNotNull'. Microsoft.EntityFrameworkCore.Database.Command[20101] Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [MigrationId], [ProductVersion] FROM [__EFMigrationsHistory] ORDER BY [MigrationId]; Microsoft.EntityFrameworkCore.Migrations[20402] Applying migration '20181113132251_AddNotNull'. Failed executing DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

CREATE TABLE [Users] (
[IDUser] int NOT NULL IDENTITY,
[Username] nvarchar(max) NOT NULL,
[Password] nvarchar(max) NOT NULL,
CONSTRAINT [PK_Users] PRIMARY KEY ([IDUser]));

System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Users' in the database. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, 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.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) ClientConnectionId:123d7ce8-1ece-4ad9-aa48-bd44c7179be1 Error Number:2714,State:6,Class:16 There is already an object named 'Users' in the database.

All i did was add [Required()] to most of my fields. And sorry for the formatting. SO thinks that the create table is a code block in this quote..

EDIT: The asnwer to the problem is that Database.EnsureCreated() method creates the database without any migration, any disables (denies?) migrations, so that's why i couldn't create any migrations.

c#
ef-core-2.1
asked on Stack Overflow Nov 13, 2018 by Tomato • edited Nov 16, 2018 by Tomato

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0