ForeignKeyConstraint FK_ModelTime_Model要求子键值(100)存在于父表中

时间:2014-12-11 15:53:12

标签: c# unit-testing foreign-keys relational-database primary-key

我收到有关“ForeignKeyConstraint FK_ModelTime_Model要求子键值(100)存在于父表中”的错误,那个子键值是数据键值吗?如何解决这些错误?

AvailabilityBLTest.cs文件

[TestMethod()]
        //--------------------------------------------------------------------------
        public void AddEffectiveQuarterTest()
        {
            AvailabilityBL target = new AvailabilityBL();
            AvailabilityDS ds = new AvailabilityDS();
            TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor accessor = new TestBusinessLogic.BusinessLogic_AvailabilityBLAccessor(target);

            // SETUP STANDARD TEST DATA
            this.SetupTestData(ds, null);
            int newModelID = -1;



            // ADD A DUMMY MODEL ROW FOR FK CONSTRAINT ADHERENCE
            this.SetupTestModel(newModelID, ds);

            // ADD DUMMY MODEL TIME ROWS
            DateTime dateKey = new DateTime(2007, 07, 01);
            this.SetupTestModelTime(newModelID, -1, dateKey, ds);
            dateKey = new DateTime(2007, 12, 30);
            this.SetupTestModelTime(newModelID, -2, dateKey, ds);

            // ADD DUMMY MODEL EVENT ROWS
            this.SetupTestModelEvent(-1, ds);
            this.SetupTestModelEvent(-1, ds);
            this.SetupTestModelEvent(-1, ds);
            this.SetupTestModelEvent(-2, ds);
            this.SetupTestModelEvent(-2, ds);

            accessor.mAvailabilityDS = ds;
            dateKey = new DateTime(2007, 09, 30);

            // CALL THE METHOD
            int actual;
            actual = target.AddEffectiveQuarter(dateKey);

            // CHECK THE RESULTS - ModelTimeID (-1 and -2 inserted above)
            int expected = -3;
            Assert.AreEqual(expected, actual, "AddEffectiveQuarter did not insert the expected ModelTimeID.");

            // CHECK THE RESULTS - Number of model events copied over (5 dummy rows + 3 copied to new quarter)
            expected = 8;
            actual = accessor.mAvailabilityDS.ModelEvent.Rows.Count;
            Assert.AreEqual(expected, actual, "AddEffectiveQuarter did not insert the expected ModelTimeID.");

            // CALL THE METHOD TO ADD IT AGAIN
            string message = string.Empty;
            try
            {
                actual = target.AddEffectiveQuarter(dateKey);
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            finally
            {
                Assert.AreNotEqual(String.Empty, message, "AddEffectiveQuarter did not throw an exception when " +
                    "an attempt was made to add a quarter that is already in the model.");
            }
        }

...

`AvailabilityDS.ModelTimeRow ModelTime1 = ds.ModelTime.NewModelTimeRow();
            ModelTime1.ModelTimeID = 101;
            ModelTime1.ModelID = 100;
            ModelTime1.DateKey = Convert.ToDateTime("2007-07-01 00:00:00.000");



            ds.ModelTime.AddModelTimeRow(ModelTime1);//error occur here



            ModelTime1 = ds.ModelTime.NewModelTimeRow();
            ModelTime1.ModelTimeID = 102;
            ModelTime1.ModelID = 101;
            ModelTime1.DateKey = Convert.ToDateTime("2008-06-29 00:00:00.000");
            ds.ModelTime.AddModelTimeRow(ModelTime1);

...`

AvailabilityDS.Designer.cs文件

namespace DataObject {


/// <summary>
///Represents a strongly typed in-memory cache of data.
///</summary>
[global::System.Serializable()]
[global::System.ComponentModel.DesignerCategoryAttribute("code")]
[global::System.ComponentModel.ToolboxItem(true)]
[global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedDataSetSchema")]
[global::System.Xml.Serialization.XmlRootAttribute("AvailabilityDS")]
[global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.DataSet")]
public partial class AvailabilityDS : global::System.Data.DataSet {

    private EventDataTable tableEvent;

    private EventTypeDataTable tableEventType;

    private LaborCategoryDataTable tableLaborCategory;

    private TimeDataTable tableTime;

    private ModelTimeDataTable tableModelTime;

    private ModelEventDataTable tableModelEvent;

    private ModelDataTable tableModel;

    private ResourceDataTable tableResource;

    private MetricDataTable tableMetric;

    private MetricDataDataTable tableMetricData;

    private ModelTimeGroupDataTable tableModelTimeGroup;

    private DurationTypeDataTable tableDurationType;

    private ModelTimeForMetricTypeDataTable tableModelTimeForMetricType;

    private ParameterInfoDataTable tableParameterInfo;

    private global::System.Data.DataRelation relationFK_ModelTime_Model;

    private global::System.Data.DataRelation relationFK_ModelEvent_ModelTimeGroup;

    private global::System.Data.DataRelation relationFK_ModelEvent_ModelTime;

    private global::System.Data.DataRelation relationFK_MetricData_Metric;

    private global::System.Data.DataRelation relationFK_ModelTimeGroup_ModelTime;

    private global::System.Data.SchemaSerializationMode _schemaSerializationMode = global::System.Data.SchemaSerializationMode.IncludeSchema;


[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
    public AvailabilityDS() {
        this.BeginInit();
        this.InitClass();
        global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
        base.Tables.CollectionChanged += schemaChangedHandler;
        base.Relations.CollectionChanged += schemaChangedHandler;
        this.EndInit();
        this.InitExpressions();
    }

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
    protected AvailabilityDS(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context) : 
            base(info, context, false) {
        if ((this.IsBinarySerialized(info, context) == true)) {
            this.InitVars(false);
            global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler1 = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
            this.Tables.CollectionChanged += schemaChangedHandler1;
            this.Relations.CollectionChanged += schemaChangedHandler1;
            if ((this.DetermineSchemaSerializationMode(info, context) == global::System.Data.SchemaSerializationMode.ExcludeSchema)) {
                this.InitExpressions();
            }
            return;
        }
        string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
        if ((this.DetermineSchemaSerializationMode(info, context) == global::System.Data.SchemaSerializationMode.IncludeSchema)) {
            global::System.Data.DataSet ds = new global::System.Data.DataSet();
            ds.ReadXmlSchema(new global::System.Xml.XmlTextReader(new global::System.IO.StringReader(strSchema)));
            if ((ds.Tables["Event"] != null)) {
                base.Tables.Add(new EventDataTable(ds.Tables["Event"]));
            }
            if ((ds.Tables["EventType"] != null)) {
                base.Tables.Add(new EventTypeDataTable(ds.Tables["EventType"]));
            }
            if ((ds.Tables["LaborCategory"] != null)) {
                base.Tables.Add(new LaborCategoryDataTable(ds.Tables["LaborCategory"]));
            }
            if ((ds.Tables["Time"] != null)) {
                base.Tables.Add(new TimeDataTable(ds.Tables["Time"]));
            }
            if ((ds.Tables["ModelTime"] != null)) {
                base.Tables.Add(new ModelTimeDataTable(ds.Tables["ModelTime"]));
            }
            if ((ds.Tables["ModelEvent"] != null)) {
                base.Tables.Add(new ModelEventDataTable(ds.Tables["ModelEvent"]));
            }
            if ((ds.Tables["Model"] != null)) {
                base.Tables.Add(new ModelDataTable(ds.Tables["Model"]));
            }
            if ((ds.Tables["Resource"] != null)) {
                base.Tables.Add(new ResourceDataTable(ds.Tables["Resource"]));
            }
            if ((ds.Tables["Metric"] != null)) {
                base.Tables.Add(new MetricDataTable(ds.Tables["Metric"]));
            }
            if ((ds.Tables["MetricData"] != null)) {
                base.Tables.Add(new MetricDataDataTable(ds.Tables["MetricData"]));
            }
            if ((ds.Tables["ModelTimeGroup"] != null)) {
                base.Tables.Add(new ModelTimeGroupDataTable(ds.Tables["ModelTimeGroup"]));
            }
            if ((ds.Tables["DurationType"] != null)) {
                base.Tables.Add(new DurationTypeDataTable(ds.Tables["DurationType"]));
            }
            if ((ds.Tables["ModelTimeForMetricType"] != null)) {
                base.Tables.Add(new ModelTimeForMetricTypeDataTable(ds.Tables["ModelTimeForMetricType"]));
            }
            if ((ds.Tables["ParameterInfo"] != null)) {
                base.Tables.Add(new ParameterInfoDataTable(ds.Tables["ParameterInfo"]));
            }
            this.DataSetName = ds.DataSetName;
            this.Prefix = ds.Prefix;
            this.Namespace = ds.Namespace;
            this.Locale = ds.Locale;
            this.CaseSensitive = ds.CaseSensitive;
            this.EnforceConstraints = ds.EnforceConstraints;
            this.Merge(ds, false, global::System.Data.MissingSchemaAction.Add);
            this.InitVars();
        }
        else {
            this.ReadXmlSchema(new global::System.Xml.XmlTextReader(new global::System.IO.StringReader(strSchema)));
            this.InitExpressions();
        }
        this.GetSerializationData(info, context);
        global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
        base.Tables.CollectionChanged += schemaChangedHandler;
        this.Relations.CollectionChanged += schemaChangedHandler;
    }

AvailabilityDS.cs文件

namespace DataObject {


partial class AvailabilityDS
{
    partial class ModelDataTable
    {
    }

    partial class EventTypeDataTable
    {
    }

    partial class ModelTimeDataTable
    {
    }

    partial class ProjectDataTable
    {
    }

    partial class ModelTimeForMetricTypeDataTable
    {
    }

    partial class ModelTimeGroupDataTable
    {
    }
}

}

1 个答案:

答案 0 :(得分:0)

似乎您已将对数据集的强制约束设置为true。

尝试:

AvailabilityDS ds = new AvailabilityDS();
ds.EnforceConstraints = false;

check here for some more info