我有一个 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 个副本。希望大家能帮忙。