Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ForeignKeyConstraint FK_ModelTime_模型要求父表中存在子键值(100)_C#_Unit Testing_Foreign Keys_Relational Database_Primary Key - Fatal编程技术网

C# ForeignKeyConstraint FK_ModelTime_模型要求父表中存在子键值(100)

C# ForeignKeyConstraint FK_ModelTime_模型要求父表中存在子键值(100),c#,unit-testing,foreign-keys,relational-database,primary-key,C#,Unit Testing,Foreign Keys,Relational Database,Primary Key,我得到了关于ForeignKeyConstraint FK_ModelTime_Model的错误要求父表中存在子键值100,子键值是数据键值吗?如何解决这些错误 可用性bltest.cs文件 ` AvailabilityDS.Designer.cs文件 }似乎您已将数据集上的约束强制设置为true 尝试: .在将“ModelTime”行添加到数据库之前,您需要在“Model”表中添加PrimaryKey=100的行。我正在使用microsoft visual studio 2012,我是第一次

我得到了关于ForeignKeyConstraint FK_ModelTime_Model的错误要求父表中存在子键值100,子键值是数据键值吗?如何解决这些错误

可用性bltest.cs文件

`

AvailabilityDS.Designer.cs文件


}

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

尝试:


.

在将“ModelTime”行添加到数据库之前,您需要在“Model”表中添加PrimaryKey=100的行。我正在使用microsoft visual studio 2012,我是第一次使用它,我不知道如何添加,我单击了属性,但没有看到要添加的值。是否需要在编码时进行修改?PK默认值是,我是否应该在那里编辑,但我收到错误消息说属性值无效原因是无法在自动递增列上设置默认值。发布创建对象的代码我没有强制约束,我如上所述放置coed,它返回错误DataObject.AvailabilityDS不包含enforceconstraints的定义,并且没有扩展方法enforceconstraints接受DataObject.AvailabilityDS类型的第一个参数。可以找到您是否缺少using指令或程序集引用?我是第一次使用它,我该怎么做才能解决这个问题?现在,使用大写锁定强制约束。应立即编译并工作错误发生在此处Assert.AreEqualexpected,actual,AddEffectiveQuarter未插入预期的ModelTimeID。;使用Assert.AreEqual失败。预期:。实际值:。AddEffectiveQuarter没有插入预期的ModelTimeID。这是另一个问题,似乎出现在代码的单元测试中。作为问题发布的约束错误已消失。
[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);
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;
    }
namespace DataObject {


partial class AvailabilityDS
{
    partial class ModelDataTable
    {
    }

    partial class EventTypeDataTable
    {
    }

    partial class ModelTimeDataTable
    {
    }

    partial class ProjectDataTable
    {
    }

    partial class ModelTimeForMetricTypeDataTable
    {
    }

    partial class ModelTimeGroupDataTable
    {
    }
}
AvailabilityDS ds = new AvailabilityDS();
ds.EnforceConstraints = false;