- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的模型如图所示。
代码:
App MyApp = (App)Application.Current;
WFE = new WorkflowMenuEntities();
WFE.Database.Connection.ConnectionString = MyApp.WorkflowMenuEntityConnectionString;
string Input = Microsoft.VisualBasic.Interaction.InputBox("Deleting this Business Area will delete ALL processes associated with it. If you are sure you wish to continue please type in delete and press ok.", "Confirm Deletion");
if (Input == "delete")
{
int BusinessID = ((BusinessArea)ComboboxBusinessArea.SelectedItem).ID;
BusinessArea myBusinessArea = WFE.BusinessAreas.Where(BA => BA.ID == BusinessID).FirstOrDefault();
foreach (var myProcesses in myBusinessArea.Processes.ToList())
{
foreach (var myWorkerProcess in myProcesses.WorkerProcesses.ToList())
{
myProcesses.WorkerProcesses.Remove(myWorkerProcess);
}
myBusinessArea.Processes.Remove(myProcesses);
}
foreach (var myADGroup in myBusinessArea.ADGroups.ToList())
{
myBusinessArea.ADGroups.Remove(myADGroup);
}
WFE.BusinessAreas.Remove(myBusinessArea);
WFE.SaveChanges();
BuildBusinessAreaList();
}
每当我运行此代码时,它都会在 WFE.SaveChanges 行出现以下错误。
System.InvalidOperationException: 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. at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at ManageBusinessAreas.MainWindow.ButtonDeleteBusinessArea_Click(Object sender, RoutedEventArgs e) in c:\TFS_Source\SharedApps\AdminProcesses\ManageBusinessAreas\ManageBusinessAreas\ManageBusinessAreas\MainWindow.xaml.cs:line 249
我查看了其他帖子,不幸的是,这些修复要么不适用于我,要么不起作用。任何帮助将不胜感激。
最佳答案
发现问题。我不需要从 myBusinessAreas 的流程导航中删除 myProcess,而是需要将其从上下文的流程实体中删除。
关于c# - 删除实体时出现错误 : The relationship could not be changed because one or more of the foreign-key properties is non-nullable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18708631/
我是一名优秀的程序员,十分优秀!