I want to créate a new table ClientsGroup that can relate to a table that allready exists and has data Client, and when I run the migrations it gives me the error
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Clients_ClientsGroups_clientsGroupId". The conflict occurred in database "gino", table "dbo.ClientsGroups", column 'clientsGroupId'.
I have read several answers on fórums but I can not understand for example one person wrote this
Update Client set clientsGroupId = NULL
or WITH NOCHECK wich I don't know how to use in migrations
this is the migration code
public partial class _4 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "clientsGroupId",
table: "Clients",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateTable(
name: "ClientsGroups",
columns: table => new
{
clientsGroupId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
name = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ClientsGroups", x => x.clientsGroupId);
});
migrationBuilder.CreateIndex(
name: "IX_Clients_clientsGroupId",
table: "Clients",
column: "clientsGroupId");
migrationBuilder.AddForeignKey(
name: "FK_Clients_ClientsGroups_clientsGroupId",
table: "Clients",
column: "clientsGroupId",
principalTable: "ClientsGroups",
principalColumn: "clientsGroupId",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Clients_ClientsGroups_clientsGroupId",
table: "Clients");
migrationBuilder.DropTable(
name: "ClientsGroups");
migrationBuilder.DropIndex(
name: "IX_Clients_clientsGroupId",
table: "Clients");
migrationBuilder.DropColumn(
name: "clientsGroupId",
table: "Clients");
}
}