En .NET por defecto se eliminan los registros relacionados en cascada. Para evitarlo antes de actualizar la base de datos tenemos que cambiar la migración:
constraints: table =>
{
table.PrimaryKey("PK_ProfesionalesActividades", x => x.Id);
table.ForeignKey(
name: "FK_ProfesionalesActividades_Actividad_ActividadId",
column: x => x.ActividadId,
principalTable: "Actividad",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ProfesionalesActividades_Profesionales_ProfesionalId",
column: x => x.ProfesionalId,
principalTable: "Profesionales",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
Para que por defecto sea así en todas las relaciones lo tenemos que poner en el contexto:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (var foreignKey in modelBuilder.Model.GetEntityTypes()
.SelectMany(e => e.GetForeignKeys()))
{
foreignKey.DeleteBehavior = DeleteBehavior.Restrict;
}
}
Y si la cosa ya está hecha añadimos una migración a mano:
In the Package Manager Console, create a new, empty migration with the Add-Migration command, then fill in the Up method like this:
migrationBuilder.DropForeignKey(
name: "FK_ElementsPerStrip_Strips_StripId",
table: "ElementsPerStrip");
migrationBuilder.AddForeignKey(
name: "FK_ElementsPerStrip_Strips_StripId",
table: "ElementsPerStrip",
column: "StripId",
principalTable: "Strips",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
For completeness, do the opposite in the Down method:
migrationBuilder.DropForeignKey(
name: "FK_ElementsPerStrip_Strips_StripId",
table: "ElementsPerStrip");
migrationBuilder.AddForeignKey(
name: "FK_ElementsPerStrip_Strips_StripId",
table: "ElementsPerStrip",
column: "StripId",
principalTable: "Strips",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);