实体框架中删除问题
我在实体框架 4 中映射了一个表,它是其他几个表的子表。每当尝试删除该表中的一行时,我都会收到以下消息。该表具有其他表的多个外键,但它始终是关系中的子表,而不是主表。当我调用 context.DeleteObject(object) 时,就会出现此消息,它不需要调用 context.SaveChanges() 。我已经验证所有关系都在 .edmx 设计器中正确定义。
消息: 操作失败:无法更改关系,因为一个或多个外键属性不可为空。当关系发生更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
表
Project (1) - (many) ProjectMember (many) - (1) Employee
ProjectID (PK) ProjectMemberID (PK) EmployeeID (PK)
ProjectName (varchar) ProjectID (FK) FirstName (varchar)
EmployeeID (FK) LastName (varchar)
Role (varchar)
对于每个项目,我都有多个来自公司员工列表的项目成员。删除 ProjectMember 应该不会影响 Project 或 Employee 表。
我不希望 FK 可为空,因为关系必须存在。
当在行上点击删除时,将从 WPF 4 数据网格中进行删除。我拦截删除键并调用 context.DeleteObject( )。
CommandManager.AddPreviewExecutedHandler(grid, new ExecutedRoutedEventHandler(gridDeleteCommandHandler));
private void gridDeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == DataGrid.DeleteCommand)
{
if (grid.SelectedItem is ProjectMember)
{
ProjectMember pm = (ProjectMember)grid.SelectedItem;
_context.DeleteObject(pm);
SaveChanges();
}
}
e.Handled = true;
}
关于为什么会发生错误以及如何让删除工作有什么想法吗?如果子表中没有依赖行,则从主表中删除没有问题
I have a table mapped in the Entity Framework 4 that is the child table to a few other tables. Any time a try to delete a row in that table I get the message below. This table has multiple foreign keys to other tables but it is always the child table in the relationship, never the primary. This message happens as soon as I call context.DeleteObject(object), it doesn't need the context.SaveChanges() called. I have verified that all the relationships are defined correctly in the .edmx designer.
Message:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Tables
Project (1) - (many) ProjectMember (many) - (1) Employee
ProjectID (PK) ProjectMemberID (PK) EmployeeID (PK)
ProjectName (varchar) ProjectID (FK) FirstName (varchar)
EmployeeID (FK) LastName (varchar)
Role (varchar)
For every Project, I have multiple ProjectMembers which come from the list of Employees in the company. Deleting a ProjectMember should have no affect on either the Project or Employee table.
I don't want the FKs to be nullable because the relation must exist.
The deletion happens from a WPF 4 datagrid when delete is hit on the row. I intercept the delete key and call context.DeleteObject( ).
CommandManager.AddPreviewExecutedHandler(grid, new ExecutedRoutedEventHandler(gridDeleteCommandHandler));
private void gridDeleteCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == DataGrid.DeleteCommand)
{
if (grid.SelectedItem is ProjectMember)
{
ProjectMember pm = (ProjectMember)grid.SelectedItem;
_context.DeleteObject(pm);
SaveChanges();
}
}
e.Handled = true;
}
Any ideas on why the error is occurring and how do I get the delete to work? I have no problem deleting from the primary table if there are no dependent rows in the child
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了。为了获取这些数据,我在屏幕上有两个网格。第一个列出了所有项目,第二个列出了所选项目的项目成员。我将使用与填充项目网格相同的上下文来填充 ProjectMember 网格。不知何故,我不明白这部分,产生错误的关系将被建立并强制执行。但是,如果我使用不同的上下文填充 ProjectMember 网格,我就可以按照您的预期删除 ProjectMember。我不知道为什么实体框架会这样做,但它对我有用。
Found the problem. To get to this data, I had two grids on the screen. The first listed all the Projects, the second would list the ProjectMembers for the selected Project. I would populate the ProjectMember grid using the same context that was used to populate the Project grid. Somehow, and I don't understand this part, the relationship that produced the error would be established and enforced. But, if I populate the ProjectMember grid using a different context, I am able to delete the ProjectMembers as you would expect. I do not know why the Entity Framework behaves this way but it works for me.