Efcore: Invalid column name in unique constrain causes to generate drop all tables while adding migration

Created on 12 Jun 2019  路  6Comments  路  Source: dotnet/efcore

this line => entityBuilder.Entity<IEntity>().HasIndex(new [] { "Field1" , "Field2" }).IsUnique(true);

Field 2 is invalid and is not available in the table but added to the above Fluent API line by human mistake !!!!

when adding migration it will generate a migration file that contains Drop All Tables !!!!!!

screenshot of the generated migration file

Further technical details

EntityFrameworkCore version: 2.2.4
Database Provider: (Microsoft.EntityFrameworkCore.SqlServer)
Operating system:
IDE: (Visual Studio 2019 community 16.2)

please need help !

closed-question customer-reported

All 6 comments

@oOAhmedKingOo Please post a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

sorry for late replay,
here is a runnable project attached

click here to download the project

just add migration on TestDbContext and see what will happen

BTW im using Reflection to do migrations !!

@oOAhmedKingOo The model building code is throwing this:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: The specified type 'WebApplication1.IEntity'must be a non-interface reference type to be used as an entity type .

but the exception is being swallowed in your OnModelCreating method. This leaves the model in an invalid state.

ohh didn't notice that, thanks for your help, and sorry for late replay :)

Was this page helpful?
0 / 5 - 0 ratings