gpt4 book ai didi

c# - 删除实体时出现错误 : The relationship could not be changed because one or more of the foreign-key properties is non-nullable

转载 作者:行者123 更新时间:2023-11-30 17:03:20 25 4
gpt4 key购买 nike

我的模型如图所示。

enter image description here

代码:

            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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com