Entity framework parent -> child linking and foreign key constraint failed 错误
Entity framework parent -> child linking and foreign key constrain failed error
我正在使用 entity framework 7(核心)和 Sqlite 数据库。当前使用组合框通过此方法更改实体类别:
/// <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;
}
编辑
以下是 device
和 category
的结构:
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 table 有外键限制 ON DELETE RESTRICT
。这意味着如果 Devices 中的任何行仍然指向您要删除的类别,SQLite 数据库将阻止此操作。要解决此问题,您可以 (1) 明确地将所有设备更改为不同的类别或 (2) 将 ON DELETE 行为更改为其他内容,例如 CASCADE
或 SET NULL
。参见 https://www.sqlite.org/foreignkeys.html#fk_actions
如果您使用 EF 创建了 tables,请将您的模型配置为使用不同的删除行为。默认是限制。参见 https://docs.efproject.net/en/latest/modeling/relationships.html#id2。示例:
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.OnDelete(DeleteBehavior.Cascade);
我正在使用 entity framework 7(核心)和 Sqlite 数据库。当前使用组合框通过此方法更改实体类别:
/// <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;
}
编辑
以下是 device
和 category
的结构:
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 table 有外键限制 ON DELETE RESTRICT
。这意味着如果 Devices 中的任何行仍然指向您要删除的类别,SQLite 数据库将阻止此操作。要解决此问题,您可以 (1) 明确地将所有设备更改为不同的类别或 (2) 将 ON DELETE 行为更改为其他内容,例如 CASCADE
或 SET NULL
。参见 https://www.sqlite.org/foreignkeys.html#fk_actions
如果您使用 EF 创建了 tables,请将您的模型配置为使用不同的删除行为。默认是限制。参见 https://docs.efproject.net/en/latest/modeling/relationships.html#id2。示例:
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.OnDelete(DeleteBehavior.Cascade);