C# 用户代码未处理无效的约束异常
我创建了一个测试用例调用GetChangeSetPerProjectTest,下面是该测试用例的代码:C# 用户代码未处理无效的约束异常,c#,unit-testing,C#,Unit Testing,我创建了一个测试用例调用GetChangeSetPerProjectTest,下面是该测试用例的代码: public void GetChangeSetPerProjectTest() { CommonPHIEditorBL_Accessor target = new CommonPHIEditorBL_Accessor(); target.mCommonPHIEditorDS = new CommonPHIEditorDS(); AddP
public void GetChangeSetPerProjectTest()
{
CommonPHIEditorBL_Accessor target = new CommonPHIEditorBL_Accessor();
target.mCommonPHIEditorDS = new CommonPHIEditorDS();
AddPHIYieldInfoREBI(target.mCommonPHIEditorDS.PHIYieldParm, 2, 2, 2, 2, 11705, 2, 2);
AddPHIYieldInfoREBI(target.mCommonPHIEditorDS.PHIYieldParm, 2, 3, 3, 3, 11705, 3, 3);
target.mCommonPHIEditorDS.PHIYieldParm.Rows[1].AcceptChanges();
target.mCommonPHIEditorDS.PHIYieldParm.Rows[1].SetModified();
//For DATA
AddPHIYieldDataInfoWithItemID(target.mCommonPHIEditorDS.PHIYieldParmData, 2, Convert.ToDateTime("2010-06-20 00:00:00.000"), 2, 2, 2, 2, 11705, 2);
AddPHIYieldDataInfoWithItemID(target.mCommonPHIEditorDS.PHIYieldParmData, 3, Convert.ToDateTime("2010-06-20 00:00:00.000"), 3, 3, 3, 3, 11705, 2); //error happened at here
//For Comment Data
EUPHModelDS.MetricModelCommentDTDataTable newDt = new EUPHModelDS.MetricModelCommentDTDataTable();
AddEUPHModelDSDataSet(newDt, "MetricModelCommentDT", 2, 2, 2);
AddEUPHModelDSDataSet(newDt, "MetricModelCommentDT", 4, 2, 2);
AddEUPHModelDSDataSet(newDt, "MetricModelCommentDT", 3, 3, 3);
target.mCommonPHIEditorDS.Tables.Add(newDt);
int projectID = 2; // TODO: Initialize to an appropriate value
int expected; // TODO: Initialize to an appropriate value
expected = target.mCommonPHIEditorDS.PHIYieldParm.Rows.Count;
CommonPHIEditorDS actual;
actual = target.GetChangeSetPerProject(projectID);
Assert.AreEqual(expected, actual.PHIYieldParm.Rows.Count);
Assert.AreEqual(2, actual.PHIYieldParmData.Rows.Count);
Assert.AreEqual(1, actual.Tables["MetricModelCommentDT"].Rows.Count);
//Assert.Inconclusive("Verify the correctness of this test method.");
}
但是,我在检查以下第二个数据时出错:
添加PhiyieldDataInfoWithItemId(target.mCommonPHIEditorDS.PHIYieldParmData,3,Convert.ToDateTime(“2010-06-20 00:00:00.000”),3,3,3,3,11705,2)
在AddPHIYieldDataInfoWithItemID中:
private void AddPHIYieldDataInfoWithItemID(CommonPHIEditorDS.PHIYieldParmDataDataTable dt, int dataID, DateTime dateKey, int metricID, int inputID, int metricModelInputID, int horizonID, int ItemID, int MetricModelID)
{
CommonPHIEditorDS.PHIYieldParmDataRow newRow = dt.NewPHIYieldParmDataRow();
newRow.DataID = dataID;
newRow.DateKey = dateKey;
newRow.InputID = inputID;
newRow.MetricModelInputID = metricModelInputID;
newRow.ItemID = ItemID;
newRow.HrzDisplay = "Current Quater";
newRow.HorizonID = horizonID;
newRow.Value = 20.7;
newRow.MetricID = metricID;
newRow.MetricModelID = 2;
dt.Rows.Add(newRow);
}
我可以成功地通过第一个数据,但在第二个数据中得到错误,即:
ForeignKeyConstraint FK_PHIYieldParm_PHIYieldParmData要求父表中存在子键值(3,3,3,2,11705)
这些值是否存在于父表中?@paqogomez,我的父表是“AddPHIYieldDataInfoWithItemID(target.mCommonPHIEditorDS.PHIYieldParmData,3,Convert.ToDateTime(“2010-06-20 00:00:00.000”),3,3,3,11705,2)”,但我尝试用AddPHIYieldDataInfoWithItemID替换(target.mCommonPHIEditorDS.PHIYieldParmData,3,Convert.ToDateTime(“2010-06-20 00:00:00.000”),3,3,3,2,11705,2);,仍然会收到相同的错误,显示如何在设置数据库时禁用约束。请确保表中实际存在这些值…(表,不是函数)