gpt4 book ai didi

c# - Entity Framework 父 -> 子链接和外键约束失败错误

转载 作者:行者123 更新时间:2023-12-03 19:35:41 26 4
gpt4 key购买 nike

我正在使用 Entity Framework 7(核心)和 Sqlite 数据库。当前使用 comboBox 通过此方法更改实体类别:

/// <summary>
/// Changes the given device gategory.
/// </summary>
/// <param name="device"></param>
/// <param name="category"></param>
public bool ChangeCategory(Device device, Category category)
{
if (device != null && category != null )
{
try
{
var selectedCategory = FxContext.Categories.SingleOrDefault(s => s.Name == category.Name);
if (selectedCategory == null) return false;
if (device.Category1 == selectedCategory) return true;

device.Category1 = selectedCategory;
device.Category = selectedCategory.Name;
device.TimeCreated = DateTime.Now;
return true;
}
catch (Exception ex)
{
throw new InvalidOperationException("Category change for device failed. Possible reason: database has multiple categories with same name.");
}
}
return false;
}

此函数可以很好地更改 device 的类别 ID。但这是正确的方法吗?

链接后,稍后在删除此 category 时,我从 Sqlite 数据库中收到错误消息:

{"SQLite Error 19: 'FOREIGN KEY constraint failed'"}

删除类别的方法

public bool RemoveCategory(Category category)
{
if (category == null) return false;
var itemForDeletion = FxContext.Categories
.Where(d => d.CategoryId == category.CategoryId);
FxContext.Categories.RemoveRange(itemForDeletion);
return true;
}

编辑以下是 devicecategory 的结构:

CREATE TABLE "Category" (
"CategoryId" INTEGER NOT NULL CONSTRAINT "PK_Category" PRIMARY KEY AUTOINCREMENT,
"Description" TEXT,
"HasErrors" INTEGER NOT NULL,
"IsValid" INTEGER NOT NULL,
"Name" TEXT
)

CREATE TABLE "Device" (
"DeviceId" INTEGER NOT NULL CONSTRAINT "PK_Device" PRIMARY KEY AUTOINCREMENT,
"Category" TEXT,
"Category1CategoryId" INTEGER,
CONSTRAINT "FK_Device_Category_Category1CategoryId" FOREIGN KEY ("Category1CategoryId") REFERENCES "Category" ("CategoryId") ON DELETE RESTRICT,
)

最佳答案

您的 SQLite 表有外键限制 ON DELETE RESTRICT。这意味着如果 Devices 中的任何行仍然指向您要删除的类别,SQLite 数据库将阻止此操作。要解决此问题,您可以 (1) 明确地将所有设备更改为不同的类别,或者 (2) 将 ON DELETE 行为更改为其他内容,例如 CASCADESET NULL。参见 https://www.sqlite.org/foreignkeys.html#fk_actions

如果您已使用 EF 创建表,请将您的模型配置为使用不同的删除行为。默认是限制。参见 https://docs.efproject.net/en/latest/modeling/relationships.html#id2 .示例:

        modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.OnDelete(DeleteBehavior.Cascade);

关于c# - Entity Framework 父 -> 子链接和外键约束失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36010867/

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