Я использую EF Core для создания миграций.
Код: Выделить всё
migrationBuilder.CreateTable(
name: "ElementsPerStrip",
columns: table => new
{
......
},
constraints: table =>
{
table.PrimaryKey("PK_ElementsPerStrip", x => x.Id);
table.ForeignKey(
name: "FK_ElementsPerStrip_Strips_StripId",
column: x => x.StripId,
principalTable: "Strips",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
в этом, как моя модель полоса выглядит:
public class Strip
{
public int Id { get; set; }
public int ProjectId { get; set; }
public int ScriptId { get; set; }
public virtual Script Script { get; set; }
public int SceneId { get; set; }
...
public virtual ICollection ElementPerStrip { get; set; }
}
< /code>
А так выглядит мой элемент -elementperstrip: < /p>
public class ElementPerStrip
{
public int Id { get; set; }
...
public int StripId { get; set; }
// public virtual Strip strip { get; set; } // issue is here
}
< /code>
и в моделе Builder я добавил это в попытке изменить DeleteBehavior, чтобы ограничить: < /p>
builder.Entity()
.HasMany(c => c.ElementPerStrip)
.WithOne()
.HasForeignKey(c => c.StripId)
.OnDelete(DeleteBehavior.Restrict);
< /code>
Миграция, которую он создает, выглядит правильно для меня: < /p>
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);
< /code>
Но когда я запускаю свое приложение и пытаюсь удалить полосу, я получаю следующую ошибку: < /p>
The DELETE statement conflicted with the REFERENCE constraint "FK_ElementsPerStrip_Strips_StripId". The conflict occurred in database "MDb", table "dbo.ElementsPerStrip", column 'StripId'.
< /code>
Я не могу удалить столбец Stripid, потому что мне нужны данные в целях фильтрации в приложении.>
Подробнее здесь: https://stackoverflow.com/questions/633 ... code-first