Error Number:547,State:0,Class:16 при попытке обновить базу данных
Всем привет. При попытке обновить базу данных вылезает эта ошибка:
Error Number:547,State:0,Class:16 The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Product_ApplicationType_ApplicationId". The conflict occurred in database "Rocky", table "dbo.ApplicationType", column 'Id'. PM>
Что делать?
20220828104535_AddApplicationTypeToProduct.cs
using Microsoft.EntityFrameworkCore.Migrations;
namespace Rocky.Migrations
{
public partial class AddApplicationTypeToProduct : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ApplicationId",
table: "Product",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.CreateIndex(
name: "IX_Product_ApplicationId",
table: "Product",
column: "ApplicationId");
migrationBuilder.AddForeignKey(
name: "FK_Product_ApplicationType_ApplicationId",
table: "Product",
column: "ApplicationId",
principalTable: "ApplicationType",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Product_ApplicationType_ApplicationId",
table: "Product");
migrationBuilder.DropIndex(
name: "IX_Product_ApplicationId",
table: "Product");
migrationBuilder.DropColumn(
name: "ApplicationId",
table: "Product");
}
}
}
Модель applicationtype.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Rocky.Models
{
public class ApplicationType
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
}
Ответы (2 шт):
" [Display(Name="Application Type")] public int ApplicationTypeId { get; set; } [ForeignKey("ApplicationTypeId")] public virtual ApplicationType ApplicationType { get; set; } " use ApplicationTypeId instead of ApplicationId
удали все данные из таблицы (если это учебный проект то это не критично) и выполни update-database. У меня все сработало