Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 5.0, Preview 4. Enum_Constraint repeats three times. Update-Database fails #21004

Closed
henrikdahl8240 opened this issue May 20, 2020 · 4 comments

Comments

@henrikdahl8240
Copy link

Update-Database fails with the information:

PM> Update-Database -Context XXX_DbContext -Project XXX.Migrations -StartupProject XXX
Build started...
Build succeeded.
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='600']
      ALTER TABLE [L].[MyTable] ADD CONSTRAINT [CK_MyTable_MyColumn_Enum_Constraint] CHECK ([MyColumn] IN(0, 1, 2));
Failed executing DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='600']
ALTER TABLE [L].[MyTable] ADD CONSTRAINT [CK_MyTable_MyColumn_Enum_Constraint] CHECK ([MyColumn] IN(0, 1, 2));
Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'CK_MyTable_MyColumn_Enum_Constraint' in the database.
Could not create constraint or index. See previous errors.
   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)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, 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(TaskCompletionSource`1 completion, Boolean 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(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:d4f9b048-76a6-4274-8abb-97bca4430e91
Error Number:2714,State:5,Class:16
There is already an object named 'CK_MyTable_MyColumn_Enum_Constraint' in the database.
Could not create constraint or index. See previous errors.
PM> 

Using Script-Migrations I can observe, that there are three occurrences of:
ALTER TABLE [MySchema].[MyTable] ADD CONSTRAINT [CK_MyTable_MyColumn_Enum_Constraint] CHECK ([MyColumn] IN(0, 1, 2));

I have tried to check approximately 10 different ALTER TABLE [MySchema].[MyTable] ADD CONSTRAINT [CK_..._..._Enum_Constraint] CHECK ([.] IN(...)); statements.They were all repeated 3 times each.

Is there an error in EF Core 5.0 Preview 4 that these ...Enum_Constraint constraints are simply repeated three times and I just need to wait for Preview 5.

@henrikdahl8240 henrikdahl8240 changed the title .NDT 5.0, Preview 4. Enum_Constraint repeats three times. Update-Database fails .NET 5.0, Preview 4. Enum_Constraint repeats three times. Update-Database fails May 20, 2020
@henrikdahl8240
Copy link
Author

I have noticed, that there are also situations, where the creation of the constraint is not being repeated.

@roji
Copy link
Member

roji commented May 21, 2020

@henrikdahl8240 in that case, can you please post a small, runnable code sample which triggers the repetition?

@henrikdahl8240
Copy link
Author

@roji In order to continue, I decided to remove the migrations added by former versions, simply by removing the files and folders in the Migrations folder. Afterwards I got stuck by #21010 (comment).
Therefore I don't know, if there are still generated multiple definitions. If the problems persist after I overcome the too long name of constraint issue, I will obviously aim to post such a small sample as you request.

@ajcvickers
Copy link
Member

Duplicate of #18958. Note however that automatic generation of these constraints has been removed from 5.0 and moved to an external package--see #20897.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants