DataContext似乎从数据库中提取了相同记录的2个副本

时间:2013-02-04 15:57:53

标签: c# linq linq-to-sql datacontext

我有一个DataContext,似乎是两个从数据库中拉出同一记录的两个副本。数据库中只有这种记录。

这是执行此操作的代码:

dataAccessContext =
                    new DataAccessDataContext("Data Source=" + SettingsManager.OpsMgrSettings.DBDIR +
                                              ";default lock timeout=10000");

当我在调用上面的内容后进行dataAccessContext的调试时,我会看一个名为JobNumbers的实体。在该实体内部,有另一个名为SafetyIssues的实体,它有2个记录而不是1.它们是完全相同的记录。数据库只显示一条记录,但有些记录是如何在dataAccessContext中提取两次相同的记录。

即使我稍后绑定到列表框,它也会在列表框中显示两次相同的记录。如果我点击一个,他们就会被选中。

以下是JobNumbers实体的dbml设计(如果这有帮助):

  <Table Name="JobNumber" Member="JobNumbers">
    <Type Name="JobNumber">
      <Column Name="JobID" AutoSync="OnInsert" Type="System.Int32" DbType="Int NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
      <Column Name="Jobno" Type="System.String" DbType="nVarChar(50)" CanBeNull="true" />
      <Column Name="LocnID" Type="System.String" DbType="nChar(2)" CanBeNull="true" />
      <Column Name="Date" Type="System.DateTime" DbType="DateTime" CanBeNull="true" />
      <Column Name="version" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="NumJobNo" Type="System.Int32" DbType="Int" CanBeNull="true" />
      <Column Name="SQ3Username" Type="System.String" DbType="nVarChar(50)" CanBeNull="true" />
      <Column Name="SQ3Done" Type="System.Boolean" DbType="Bit" CanBeNull="true" />
      <Column Name="OrigJobNo" Type="System.String" DbType="NVarChar(20)" CanBeNull="true" />
      <Column Name="Updated" Type="System.Byte" DbType="Bit" CanBeNull="true" />
      <Column Name="Modified" Type="System.Int16" DbType="tinyint" CanBeNull="false" />
      <Column Name="JobTargetSafetySubmissions" Type="System.Int32" DbType="int" CanBeNull="false" />
      <Column Name="SQ3Completed" Type="System.Int16" DbType="smallint" CanBeNull="false" />
      <Column Name="Conflict" Type="System.Boolean" DbType="bit" CanBeNull="false" />
      <Association Name="JobNumber_EAC_Personnel" Member="EAC_Personnels" ThisKey="JobID" OtherKey="JobID" Type="EAC_Personnel" />
      <Association Name="JobNumber_La_Personnel" Member="LA_Personnel" ThisKey="JobID" OtherKey="JobID" Type="La_Personnel" />
      <Association Name="JobNumber_GWD_Header2" Member="GWD_Header2s" ThisKey="JobID" OtherKey="jobid" Type="GWD_Header2" Cardinality="One" />
      <Association Name="JobNumber_MonitorInfo" Member="MonitorInfos" ThisKey="JobID" OtherKey="JobID" Type="MonitorInfo" Cardinality="One" />
      <Association Name="JobNumber_NA_Salesman" Member="NA_Salesmans" ThisKey="JobID" OtherKey="JobID" Type="NA_Salesman" Cardinality="One" />
      <Association Name="JobNumber_NA_WRdetail" Member="NA_WRdetails" ThisKey="JobID" OtherKey="JobID" Type="NA_WRdetail" Cardinality="One" />
      <Association Name="JobNumber_NA_BillingCompany" Member="NA_BillingCompanies" ThisKey="JobID" OtherKey="JobID" Type="NA_BillingCompany" Cardinality="One" />
      <Association Name="JobNumber_NA_Header" Member="NA_Headers" ThisKey="JobID" OtherKey="JobID" Type="NA_Header" Cardinality="One" />
      <Association Name="JobNumber_Subcontractor" Member="Subcontractors" ThisKey="JobID" OtherKey="JobID" Type="Subcontractor" Cardinality="One" />
      <Association Name="JobNumber_NA_Holedetail" Member="NA_Holedetails" ThisKey="JobID" OtherKey="JobID" Type="NA_Holedetail" Cardinality="One" />
      <Association Name="JobNumber_NA_WL_Company" Member="NA_WL_Companies" ThisKey="JobID" OtherKey="JobID" Type="NA_WL_Company" Cardinality="One" />
      <Association Name="JobNumber_Rpt_Detail" Member="Rpt_Details" ThisKey="JobID" OtherKey="JobID" Type="Rpt_Detail" Cardinality="One" />
      <Association Name="JobNumber_NA_RigDetail" Member="NA_RigDetails" ThisKey="JobID" OtherKey="JobID" Type="NA_RigDetail" Cardinality="One" />
      <Association Name="JobNumber_EAC_Header" Member="EAC_Headers" ThisKey="JobID" OtherKey="JobID" Type="EAC_Header" Cardinality="One" />
      <Association Name="JobNumber_SafetyIssue" Member="SafetyIssues" ThisKey="JobID" OtherKey="JobID" Type="SafetyIssue" />
      <Association Name="JobNumber_SafetyIssueTotals" Member="SafetyIssueTotals" ThisKey="JobID" OtherKey="JobID" Type="SafetyIssueTotals" Cardinality="One" />
      <Association Name="JobNumber_JobService" Member="JobServices" ThisKey="JobID" OtherKey="JobID" Type="JobService" />
      <Association Name="JobNumber_NA_InvoiceHeader" Member="NA_InvoiceHeaders" ThisKey="JobID" OtherKey="JobID" Type="NA_InvoiceHeader" />
      <Association Name="JobNumber_JobSQ" Member="JobSQs" ThisKey="JobID" OtherKey="JobID" Type="JobSQ" />
      <Association Name="JobNumber_LA_Header" Member="LA_Headers" ThisKey="JobID" OtherKey="JobID" Type="LA_Header" Cardinality="One" />
      <Association Name="JobNumber_LA_Invoice" Member="LA_Invoices" ThisKey="JobID" OtherKey="JobID" Type="LA_Invoice" />
      <Association Name="JobNumber_FE_extra" Member="FE_extras" ThisKey="JobID" OtherKey="JobID" Type="FE_extra" Cardinality="One" />
      <Association Name="JobNumber_FE_Header" Member="FE_Headers" ThisKey="JobID" OtherKey="JobID" Type="FE_Header" />
      <Association Name="JobNumber_FailEquipList" Member="FailEquipLists" ThisKey="JobID" OtherKey="JobID" Type="FailEquipList" />
      <Association Name="JobNumber_JobPricing" Member="JobPricings" ThisKey="JobID" OtherKey="JobID" Type="JobPricing" />
      <Association Name="JobNumber_Equip" Member="Equips" ThisKey="JobID" OtherKey="JobID" Type="Equip" />
      <Association Name="JobNumber_SafetyIssuesTotals" Member="SafetyIssuesTotals" ThisKey="JobID" OtherKey="JobID" Type="SafetyIssuesTotals" Cardinality="One" />
      <Association Name="JobNumber_EAC_Invoice" Member="EAC_Invoices" ThisKey="JobID" OtherKey="JobID" Type="EAC_Invoice" />
      <Association Name="JobNumber_RunningMode" Member="RunningModes" ThisKey="JobID" OtherKey="JobID" Type="RunningMode" Cardinality="One" />
      <Association Name="JobNumber_CorrectionsApplied" Member="CorrectionsApplieds" ThisKey="JobID" OtherKey="JobID" Type="CorrectionsApplied" Cardinality="One" />
      <Association Name="JobNumber_JobInvoice" Member="JobInvoices" ThisKey="JobID" OtherKey="JobID" Type="JobInvoice" />
      <Association Name="JobNumber_GWDRunSummaryData" Member="GWDRunSummaryDatas" ThisKey="JobID" OtherKey="jobid" Type="GWDRunSummaryData" Cardinality="One" />
      <Association Name="JobNumber_InvoiceReportingTable" Member="InvoiceReportingTables" ThisKey="JobID" OtherKey="JobID" Type="InvoiceReportingTable" />
      <Association Name="Location_JobNumber" Member="Location" ThisKey="LocnID" OtherKey="LocnID" Type="Location" IsForeignKey="true" />
    </Type>
    </Table>

这里是SateyIssues实体的dbml(如果有帮助的话):

  <Table Name="SafetyIssues">
    <Type Name="SafetyIssue">
      <Column Name="JobID" Type="System.Int32" DbType="Int" CanBeNull="true" />
      <Column Name="JobSafetyNo" Type="System.String" DbType="nVarChar(50) NOT NULL" CanBeNull="false" />
      <Column Name="SafetyCategory" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
      <Column Name="IssueDate" Type="System.DateTime" DbType="DateTime NOT NULL" CanBeNull="false" />
      <Column Name="CorrectiveActionRefNo" Type="System.String" DbType="nVarChar(50) NOT NULL" CanBeNull="false" />
      <Column Name="SafetyInitiative" Type="System.String" DbType="nVarChar(50) NOT NULL" CanBeNull="false" />
      <Column Name="Topic_Summary" Type="System.String" DbType="nVarChar(4000) NOT NULL" CanBeNull="false" />
      <Column Name="Feedback_FollowUp" Type="System.String" DbType="nVarChar(4000) NOT NULL" CanBeNull="false" />
      <Column Name="LinkID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
      <Association Name="SafetyIssue_SafetyIssueEngineer" Member="SafetyIssueEngineers" ThisKey="LinkID" OtherKey="LinkID" Type="SafetyIssueEngineer" />
      <Association Name="JobNumber_SafetyIssue" Member="JobNumber" ThisKey="JobID" OtherKey="JobID" Type="JobNumber" IsForeignKey="true" />
      <Association Name="SafetyCategory_SafetyIssue" Member="SafetyCategory1" ThisKey="SafetyCategory" OtherKey="SafetyCategoryID" Type="SafetyCategory" IsForeignKey="true" />
    </Type>
  </Table>

我写的dbml部分类代码:

    partial class SafetyIssue
    {

        partial void OnLoaded()
        {
            JobNumber.CalculateSafetyIssueTotals();  
        }

    }

    partial class SafetyIssueTotals
    {
       public void RefreshTotals(List<SafetyIssue> safetyIssues)
       {
           TotalSubmissions = safetyIssues.Count(c => c.SafetyCategory == 1);
           TotalInductions = safetyIssues.Count(c => c.SafetyCategory == 2);
           TotalSafetyMeetings = safetyIssues.Count(c => c.SafetyCategory == 3); 
           TotalToolboxTalk = safetyIssues.Count(c => c.SafetyCategory == 4);
           TotalRecognitions = safetyIssues.Count(c => c.SafetyCategory == 5);
           TotalIncidents = safetyIssues.Count(c => c.SafetyCategory == 6);
           ActualSafetySubmissions = safetyIssues.Count(c => c.JobSafetyNo.Trim() != "");
       }

    }


    partial class JobNumber:IDataErrorInfo
    {

        public void CalculateSafetyIssueTotals()
        {
            if (SafetyIssuesTotals == null)
                SafetyIssuesTotals = new SafetyIssuesTotals();

/* If I comment the part below out, it works.  Seems like if I use the SafetyIssues entity at all (in this case SafetyIssues.Count() to  assign things, the problem happens */

            SafetyIssuesTotals.TotalActualSafetySubmissions = SafetyIssues.Count;
            SafetyIssuesTotals.TotalIncidents =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 6);
            SafetyIssuesTotals.TotalInductions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 2);
            SafetyIssuesTotals.TotalRecognitions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 5);
            SafetyIssuesTotals.TotalSafetyMeetings =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 3);
            SafetyIssuesTotals.TotalSubmissions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 1);
            SafetyIssuesTotals.TotalToolboxTalk =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 4);

    //////////////////////////////////////////////////////////////////////////////////////
        }

        public bool NoReportedSafetyIssues
        {
            get
            {
                _noReportedSafetyIssues = SafetyIssues.Count == 0;
                return _noReportedSafetyIssues;
            }
            set
            {
                _noReportedSafetyIssues = value;
                SendPropertyChanged("NoReportedSafetyIssues");
            }
        }

    }

我只是看不出它为什么要拉2份相同的记录。希望你们能帮忙。

1 个答案:

答案 0 :(得分:0)

好吧,我解决了这个问题。问题出在我的dbml代码中。我觉得我在工作有机会完成加载之前计算我的总数,所以当我把计算移到工作完成加载之后,它就可以了。这是我改变的代码:

<强> BEFORE

    partial class SafetyIssue
    {

        partial void OnLoaded()
        {
            JobNumber.CalculateSafetyIssueTotals();  // I needed to move this to the JobNumber partial class
        }

    }

    partial class SafetyIssueTotals
    {
       public void RefreshTotals(List<SafetyIssue> safetyIssues)
       {
           TotalSubmissions = safetyIssues.Count(c => c.SafetyCategory == 1);
           TotalInductions = safetyIssues.Count(c => c.SafetyCategory == 2);
           TotalSafetyMeetings = safetyIssues.Count(c => c.SafetyCategory == 3); 
           TotalToolboxTalk = safetyIssues.Count(c => c.SafetyCategory == 4);
           TotalRecognitions = safetyIssues.Count(c => c.SafetyCategory == 5);
           TotalIncidents = safetyIssues.Count(c => c.SafetyCategory == 6);
           ActualSafetySubmissions = safetyIssues.Count(c => c.JobSafetyNo.Trim() != "");
       }

    }


    partial class JobNumber:IDataErrorInfo
    {

        public void CalculateSafetyIssueTotals()
        {
            if (SafetyIssuesTotals == null)
                SafetyIssuesTotals = new SafetyIssuesTotals();


            SafetyIssuesTotals.TotalActualSafetySubmissions = SafetyIssues.Count;
            SafetyIssuesTotals.TotalIncidents =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 6);
            SafetyIssuesTotals.TotalInductions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 2);
            SafetyIssuesTotals.TotalRecognitions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 5);
            SafetyIssuesTotals.TotalSafetyMeetings =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 3);
            SafetyIssuesTotals.TotalSubmissions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 1);
            SafetyIssuesTotals.TotalToolboxTalk =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 4);

        }

    }

<强>后

    partial class SafetyIssue
    {

    }

    partial class SafetyIssueTotals
    {
       public void RefreshTotals(List<SafetyIssue> safetyIssues)
       {
           TotalSubmissions = safetyIssues.Count(c => c.SafetyCategory == 1);
           TotalInductions = safetyIssues.Count(c => c.SafetyCategory == 2);
           TotalSafetyMeetings = safetyIssues.Count(c => c.SafetyCategory == 3); 
           TotalToolboxTalk = safetyIssues.Count(c => c.SafetyCategory == 4);
           TotalRecognitions = safetyIssues.Count(c => c.SafetyCategory == 5);
           TotalIncidents = safetyIssues.Count(c => c.SafetyCategory == 6);
           ActualSafetySubmissions = safetyIssues.Count(c => c.JobSafetyNo.Trim() != "");
       }

    }


    partial class JobNumber:IDataErrorInfo
    {

        partial void OnLoaded()
        {
           CalculateSafetyIssueTotals(); //moved the call to the function here. 
        }

        public void CalculateSafetyIssueTotals()
        {
            if (SafetyIssuesTotals == null)
                SafetyIssuesTotals = new SafetyIssuesTotals();


            SafetyIssuesTotals.TotalActualSafetySubmissions = SafetyIssues.Count;
            SafetyIssuesTotals.TotalIncidents =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 6);
            SafetyIssuesTotals.TotalInductions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 2);
            SafetyIssuesTotals.TotalRecognitions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 5);
            SafetyIssuesTotals.TotalSafetyMeetings =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 3);
            SafetyIssuesTotals.TotalSubmissions =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 1);
            SafetyIssuesTotals.TotalToolboxTalk =
                SafetyIssues.Count(s => s.SafetyCategory1 != null && s.SafetyCategory1.SafetyCategoryID == 4);

        }


    }