Delete语句与引用约束冲突 [英] Delete statement conflicts with Reference constraint
问题描述
我认为这是一个非常简单的答案。我是实体框架的新手,正在创建一个由 Contacts
和 Groups
作为对象/实体的测试应用程序。
这是我删除组的代码:
私人无效button_DeleteGroup_Click(对象发送者,EventArgs e)
{
var _selectedGroup = dataGridView_Groups.SelectedRows [0] .DataBoundItem as Group;
试试
{
cgContext.Groups.Remove(_selectedGroup);
cgContext.SaveChanges();
PopulateGroupGrid();
MessageBox.Show(已成功从数据库中删除组!);
}
catch(Exception ex){MessageBox.Show(无法从数据库中删除组。\r\n\r\n + ex); }
}
如果我删除联系人所属的组,则测试参照完整性,将抛出一个异常(应有的异常):
DELETE语句与REFERENCE约束
FK_dbo.Contacts_dbo .Groups_Group_Id。冲突发生在
数据库 ContactGroups的表 dbo.Contacts的
列 Group_Id中。该语句已终止。
然后,我捕获此异常并向用户显示一条消息。如果随后我要添加一个新的组或联系人或执行任何操作,则该事务将失败,并具有与以前相同的例外:
该语句与REFERENCE约束
FK_dbo.Contacts_dbo.Groups_Group_Id发生冲突。该冲突发生在
数据库 ContactGroups的表 dbo.Contacts的列
'Group_Id'中。被终止。
因此,很明显,当发生初始异常时,我不会清除/结束事务或其他操作。我在做什么错或遗漏?
解决方案将WillCascadeOnDelete(true)用作WithOptional
model_modeler.Entity< Parent>()
.HasMany< Child>(c => c.Children)
.WithOptional(x => ; x.Parent)
.WillCascadeOnDelete(true);
使用Include
var adv = db.Adv.Include(b => b。功能)
.Include(b => b.AdvDetails)
.Include(b => b。 AdvGallery)
.FirstOrDefault(b => b.Id == id);
db.Adv.Remove(adv);
for .HasMany(...)。WithMany(... )可以了
I assume this has a very simple answer. I am brand new to entity framework and I am creating a test application consisting of
Contacts
andGroups
as objects/entities.Here is my code to delete a group:
private void button_DeleteGroup_Click(object sender, EventArgs e) { var _selectedGroup = dataGridView_Groups.SelectedRows[0].DataBoundItem as Group; try { cgContext.Groups.Remove(_selectedGroup); cgContext.SaveChanges(); PopulateGroupGrid(); MessageBox.Show("Successfully deleted group from database!"); } catch(Exception ex) { MessageBox.Show("Failed to delete group from database.\r\n\r\n" + ex); } }
If I delete a group that a contact belongs to, to test referential integrity, an exception is thrown (as it should):
"The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Contacts_dbo.Groups_Group_Id". The conflict occurred in database "ContactGroups", table "dbo.Contacts", column 'Group_Id'. The statement has been terminated."
I then catch this exception and display a message to the user. If I then go to add a new group or contact or do anything, the transaction fails with the same exception as before:
"The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.Contacts_dbo.Groups_Group_Id". The conflict occurred in database "ContactGroups", table "dbo.Contacts", column 'Group_Id'. The statement has been terminated."
So, obviously i'm not clearing / ending a transaction or something when the initial exception occurs. What am I doing wrong or missing?
解决方案use WillCascadeOnDelete(true) for WithOptional
modelBuilder.Entity<Parent>() .HasMany<Child>(c => c.Children) .WithOptional(x => x.Parent) .WillCascadeOnDelete(true);
use Include
var adv = db.Adv.Include(b => b.Features) .Include(b => b.AdvDetails) .Include(b => b.AdvGallery) .FirstOrDefault(b => b.Id == id); db.Adv.Remove(adv);
for .HasMany(...).WithMany(...) Include is ok
这篇关于Delete语句与引用约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!