ObjectDeletedException:删除的对象将通过级联重新保存(从关联中删除已删除的对象)

时间:2018-07-23 19:28:45

标签: c# nhibernate

我继承了这段代码,之前从未使用过Nhibernate。当我到达以下代码行时,我将收到级联错误(从关联中删除已删除的对象)错误,将重新保存已删除的对象: FactoryTools.AimsFactory.Update(CurProject);

我已经尝试了其他一些帖子的内容,但没有任何效果。

        public void DeleteProjectActivity(long? pId, bool pIsDeletionFromActivityList)
    {
        //**7-Jul-2016
        //**From AIMS ProjectView.aspx.cs - grdActvity_RowDeleting()
        //************************************************************
        if (CurProject == null) //**Retrieve the current project
        {
            CurProject = AimsHelper.ProjectById(ProjectId);

            if (CurProject == null) //**Invalid project
            {
                return;
            }
        }

        var activity =
            CurProject.Activities.FirstOrDefault(x => x.Id == pId); //**Retrieve the activity based on the id received

        if (activity == null) //**Activity not found - try the activity log directly
        {
            activity = AimsHelper.ActivityLogById(pId);

            if (activity == null) //**Invalid activity id
            {
                return;
            }
        }

        CurProject.Facility.UpdatedDate = DateTime.Now.Date;
        if (activity.Type.Code == ActivityType.FINAL_DETERMINATION)
        {
            RemoveDeadlineToContestActivity(); //**Remove the DeadlineToContest activity if final action is permit issued
            CurProject.Activities.Remove(activity); //**Remove the activity from the project    
        }

        var publicComment = new PublicCommentPeriod();

        if (CurProject.PublicCommentPeriods.Count > 0)                          //**Retrieve the current public comment -> if it exists
        {
            publicComment = CurProject.PublicCommentPeriods[0];
        }

      //** An ActivityList delete was clicked; both the public comment form data and the ActivityList needs updated
      //** This really should be refactored into a usable method
      //** Request via Trello card for PC Period & Opportunity to be treated independently.
        if (pIsDeletionFromActivityList)
        {
            if (activity.Type.Id != null)
            {
                var activityId = (Int32)activity.Type.Id;
                switch (activityId)
                {
                    case 6:
                        PublicCommentOpportunityCommentPeriodRequested = false;
                        PublicCommentOpportunityCommentsReceived = false;
                        PublicCommentOpportunityStart = null;
                        PublicCommentOpportunityEnd = null;
                        //publicComment.Activity.Type.Id = null;
                        CurProject.PublicCommentPeriods[0].Opportunity = null;
                        publicComment.IsRequested = false;
                        publicComment.AreOpportunityCommentsReceived = false;
                        break;

                    case 7:
                        //** Clear view entries
                        PublicCommentCommentsReceived = false;
                        PublicCommentHearingRequested = false;
                        PublicCommentStart = null;
                        PublicCommentEnd = null;
                        PublicCommentDocketNumber = string.Empty;
                        //** Clear db values
                        publicComment.AreCommentsReceived = false;
                        publicComment.IsHearingRequested = false;
                        CurProject.PublicCommentPeriods[0].DocketNumber = string.Empty;
                        //publicComment.Activity.Type.Id = null;
                        CurProject.PublicCommentPeriods[0].Activity = null;
                        break;
                    case 9:
                        if (CurProject.PublicCommentPeriods[0].Hearing != null)
                        {
                            CurProject.PublicCommentPeriods[0].Hearing = null;
                        }
                        break;
                    case 10:
                        CurProject.PublicCommentPeriods[0].DeadlineToContest = null;
                        break;
                    case 31:
                        UpdateVersion(CurProject);
                        break;
                    case 32:
                        UpdateVersion(CurProject);
                        break;
                    default:
                        break;
                }

            }

            if (publicComment.Hearing != null)
            {
                PublicCommentHearingLocation = string.Empty;
                PublicCommentHearingDate = null;
            }
        }

        activity.Project = null;
        activity.Type = null;
        //var activityChildren = activity.Children.Count;
        //if (activityChildren > 0)
        //{
        //       activity.Children.Remove();
        //}

        CurProject.Activities.Remove(activity);
        FactoryTools.AimsFactory.Update(CurProject);                            //**Update changes to the project
    }

这里是映射。抱歉,直到现在我才知道这个文件。

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
assembly="gov.idaho.deq.air.aims.dbms" namespace="gov.idaho.deq.air.aims.dbms" >
  <class name="ActivityLog" table="D_ACTIVITY_LOG">
    <id name="Id">
      <generator class="identity"/>
    </id>

    <property name="Description"/>
    <property name="ScheduledStartDate" column="SCHEDULED_START_DATE"/>
    <property name="ActualStartDate" column="ACTUAL_START_DATE"/>
    <property name="ScheduledEndDate" column="SCHEDULED_END_DATE"/>
    <property name="ActualEndDate" column="ACTUAL_END_DATE"/>

    <many-to-one name="Type" column="L_ACTIVITY_TYPE_ID" class="ActivityType"/>
    <many-to-one name="Parent" class="ActivityLog"/>

    <bag name="Children" table="D_ACTIVITY_LOG" cascade="all-delete-orphan" lazy="true">
      <key column="PARENT"/>
      <one-to-many class="ActivityLog"/>
    </bag>

    <bag name="Projects" table="D_PROJECT_ACTIVITY" lazy="true" cascade="all-delete-orphan">
      <key column="D_ACTIVITY_LOG_ID"/>
      <many-to-many class="Project" column="D_PROJECT_ID"/>
    </bag>

    <bag name="Comments" table="D_ACTIVITY_LOG_COMMENT" cascade="all-delete-orphan" inverse="true" lazy="true">
      <key column="D_ACTIVITY_LOG_ID"/>
      <one-to-many class="ActivityLogComment"/>
    </bag>

    <!-- <bag name="FacilityInspections" table="D_FACILITY_INSPECTION_ACTIVITY" lazy="true">
      <key column="D_ACTIVITY_LOG_ID"/>
      <many-to-many class="FacilityInspection" column="D_FACILITY_INSPECTION_ID"/>
    </bag> -->
  </class>

  <!-- ActivityLog -->
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

问题在于在CurProject对象中的多个级别都引用了Activity对象。我必须遍历每个引用并将其删除。