I have a LenguajesPorProyecto model the PK is ProyectoId and LenguajeId properties this:
public class LenguajesPorProyecto
{
public int LenguajeId { get; set; }
public Lenguaje Lenguaje { get; set; }
public int ProyectoId { get; set; }
public Proyecto Proyecto { get; set; }
[StringLength(1)]
public char Nivel { get; set; }
}
And this restriction in fluent api:
modelBuilder.Entity<LenguajesPorProyecto>()
.HasKey(lp => new { lp.LenguajeId, lp.ProyectoId });
I have seed next seed data:
var lenguajesXProyectos = new List<LenguajesPorProyecto> {
new LenguajesPorProyecto{LenguajeId=1,ProyectoId=1,Nivel='A'},
new LenguajesPorProyecto{LenguajeId=2,ProyectoId=1,Nivel='A'}
};
When I execute the db migration I expect that the database table accept the both items because the pair 1,1 and 1,2 is different. I need that the constraint be in both properties not only in ProyectoId.
Console message:
fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE UNIQUE INDEX [IX_LenguajesPorProyectos_ProyectoId] ON [LenguajesPorProyectos] ([ProyectoId]); Failed executing DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE UNIQUE INDEX [IX_LenguajesPorProyectos_ProyectoId] ON [LenguajesPorProyectos] ([ProyectoId]); Microsoft.Data.SqlClient.SqlException (0x80131904): La instrucción CREATE UNIQUE INDEX terminó porque se encontró una clave duplicada para el nombre de objeto 'dbo.LenguajesPorProyectos' y el nombre de índice 'IX_LenguajesPorProyectos_ProyectoId'. El valor de la clave duplicada es (1). Se terminó la instrucción. at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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, 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.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:f23a20d1-ce3d-4a9b-9112-83f84b4ae238
Error Number:1505,State:1,Class:16
La instrucción CREATE UNIQUE INDEX terminó porque se encontró una clave duplicada para el nombre de objeto 'dbo.LenguajesPorProyectos' y el nombre de índice 'IX_LenguajesPorProyectos_ProyectoId'. El valor de la clave duplicada es (1).
Se terminó la instrucción.`
User contributions licensed under CC BY-SA 3.0