[英]Entity framework parent -> child linking and foreign key constrain failed error
I'm using entity framework 7 (core) and Sqlite database. 我正在使用实体框架7(核心)和Sqlite数据库。 Currently using comboBox to change entity category with this method: 当前使用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;
}
This function changes the category Id for the device
just fine. 此功能改变为类别ID device
就好了。 But is this correct way? 但这是正确的方法吗?
After linking and then later on while deleting this category
I get an error from the Sqlite database: 链接之后,稍后再删除此category
我从Sqlite数据库收到错误:
{"SQLite Error 19: 'FOREIGN KEY constraint failed'"} {“ SQLite错误19:'FOREIGN KEY约束失败'”]}
The delete category method 删除类别方法
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;
}
EDIT Here are the structures of device
and category
: 编辑这是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,
)
Your SQLite table has a foreign key restriction ON DELETE RESTRICT
. 您的SQLite表具有ON DELETE RESTRICT
的外键限制。 This means if any row in Devices still points to the category you are trying to delete, the SQLite database will prevent this operation. 这意味着,如果“设备”中的任何行仍指向您尝试删除的类别,则SQLite数据库将阻止此操作。 To get around this, you can (1) explictly change all Devices to a different category or (2) change the ON DELETE behavior to something else, such as CASCADE
or SET NULL
. 要解决此问题,您可以(1)将所有设备显式更改为其他类别,或者(2)将ON DELETE行为更改为其他名称,例如CASCADE
或SET NULL
。 See https://www.sqlite.org/foreignkeys.html#fk_actions 参见https://www.sqlite.org/foreignkeys.html#fk_actions
If you have used EF to create your tables, then configure your model to use a different on delete behavior. 如果使用EF创建表,则将模型配置为使用其他on删除行为。 The default is restrict. 默认为限制。 See https://docs.efproject.net/en/latest/modeling/relationships.html#id2 . 请参阅https://docs.efproject.net/en/latest/modeling/relationships.html#id2 。 Example: 例:
modelBuilder.Entity<Post>()
.HasOne(p => p.Blog)
.WithMany(b => b.Posts)
.OnDelete(DeleteBehavior.Cascade);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.