gpt4 book ai didi

c# - EF 核心中的自引用错误 : the insert statement conflicted with the foreign key same table constraint

转载 作者:行者123 更新时间:2023-12-04 03:58:39 25 4
gpt4 key购买 nike

我有一个带有自引用的 Category 模型并且在创建数据库时工作正常但是当我尝试插入父类别(没有 parentId 的实体)时我得到指向外键的错误“ParentId”,但是当我在 SSMS 中使用插入脚本手动插入一个 topel 时它工作正常,并且在添加子类别(具有 parentId 的实体)时与 ef core 一起工作正常

类别模型

public class Category : ICategory
{
public Category()
{
}

[Key]
public int Id { get; set; }

[Required]
[MinLength(2)]
[MaxLength(32)]
public string Title { get; set; }

public string Icon { get; set; }

[DefaultValue(true)]
public bool IsEnabled { get; set; }

[Required]
public DateTime CreateTimeStamp { get; set; }

public DateTime LastUpdateTimeStamp { get; set; }

[ForeignKey("User")]
public int CreatorUserId { get; set; }

public int? ParentId { get; set; }

public virtual User User { get; set; }

[ForeignKey("ParentId")]
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Children { get; set; }
public virtual ICollection<Service> Services { get; set; }
}

数据库上下文

public DbSet<Category> Categories { get; set; }

modelBuilder.Entity<Category>(entity =>
{
entity.HasIndex(e => e.Title).IsUnique();
entity.HasOne(e => e.User).WithMany(e => e.CreatedCategories)
.HasForeignKey(e => e.CreatorUserId).OnDelete(DeleteBehavior.Restrict);
entity.HasOne(e => e.Parent).WithMany(e => e.Children).HasForeignKey(e => e.ParentId).IsRequired(false);
});

迁移

protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Categories",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Title = table.Column<string>(maxLength: 32, nullable: false),
Photo = table.Column<string>(nullable: true),
IsEnabled = table.Column<bool>(nullable: false),
CreateTimeStamp = table.Column<DateTime>(nullable: false),
LastUpdateTimeStamp = table.Column<DateTime>(nullable: false),
CreatorUserId = table.Column<int>(nullable: false),
ParentId = table.Column<int>(nullable: true)
}
constraints: table =>
{
table.PrimaryKey("PK_Categories", x => x.Id);
table.ForeignKey(
name: "FK_Categories_Users_CreatorUserId",
column: x => x.CreatorUserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Categories_Categories_ParentId",
column: x => x.ParentId,
principalTable: "Categories",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
}

无论如何我都可以将“ParentId”标记为可空,但 ef core 阻止了我,尽管 SQL Server 可以正常使用它!

错误信息内部异常:

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK_Categories_Categories_ParentId". The conflict occurred in database "Unknown_Db", table "dbo.Categories", column 'Id'.

最佳答案

在处理输入值的地方试试这个:

if (category.ParentId == 0)
{
category.ParentId = null;
}

关于c# - EF 核心中的自引用错误 : the insert statement conflicted with the foreign key same table constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63453813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com