Linq to sql 删除对象时出错+;一对多关系+;Linq到Sql

Linq to sql 删除对象时出错+;一对多关系+;Linq到Sql,linq-to-sql,one-to-many,Linq To Sql,One To Many,这是我的节目 对象和SectionObject之间的关系为一对多,两个表之间的关系基于ObjectId 第一:设置obj private void ObjectAdd(...) { obj = new Object(); //is Public obj = Shop.Objects.SingleOrDefault(S => S.IdObject == SelectedObjectId); } secound:在obj中添加sectionObject private voi

这是我的节目

对象和SectionObject之间的关系为一对多,两个表之间的关系基于ObjectId

第一:设置obj

private void ObjectAdd(...)
{
    obj = new Object(); //is Public
    obj = Shop.Objects.SingleOrDefault(S => S.IdObject == SelectedObjectId);
}
secound:在obj中添加sectionObject

private void BtnAddCategory(...)
{
    SectionObject SO = new SectionObject();
                SO.Section = Shop.Sections.SingleOrDefault(Sections => Sections.SectionId == FrmCategoryList.SelectedSectionId);

    obj.SectionObjects.Add(SO);
}
第三:删除obj的截面对象

private void BtnDelCategory(...)
{
    SectionObject SO = new SectionObject();
              SO = obj.SectionObjects.SingleOrDefault(Sections => Sections.SectionId == Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value));

    obj.SectionObjects.Remove(SO);
}
最终:在数据库中插入obj

private void BtnAccept(...)
{
    Shop.Objects.InsertOnSubmit(obj);
    Shop.SubmitChanges(); // ERROR
}
错误是:

System.InvalidOperationException was caught
  Message=An attempt was made to remove a relationship between a Object and a SectionObject. However, one of the relationship's foreign keys (SectionObject.ObjectId) cannot be set to null.
  Source=System.Data.Linq
  StackTrace:
       at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.SynchDependentData()
       at System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list)
       at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
       at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
       at WindowsFormsApplication1.ObjectAdd.BtnAccept_Click(Object sender, EventArgs e) in C:\Users\EasySoft\Documents\Visual Studio 2010\Projects\Shop 90.08.22\Shop10.4\Shop\ObjectAdd.cs:line 47
  InnerException: 

为对象创建sectionobject时,需要设置其sectionobject.objectid属性。
如果您在添加后立即提交更改,则会得到相同的错误。它告诉你sectionobject没有外键指向object

我使用了这段代码,但没有解决sectionobject,所以=new sectionobject();SO.ObjectId=obj.ObjectId;SO.Section=Shop.Sections.SingleOrDefault(Sections=>Sections.SectionId==FrmCategoryList.SelectedSectionId);obj.SectionObjects.Add(SO);