Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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# Can';t由于外键问题而插入新记录_C#_Sql_Linq_Tsql - Fatal编程技术网

C# Can';t由于外键问题而插入新记录

C# Can';t由于外键问题而插入新记录,c#,sql,linq,tsql,C#,Sql,Linq,Tsql,我已经看过很多关于这个错误消息的帖子,但似乎没有一篇能涵盖我的问题。错误为:“INSERT语句与外键约束冲突” 我在名为Tests的表中设置了一个外键为Null,因为不是每个记录在另一个名为api的表中都有相应的记录 在SSMS中,我可以插入一条新记录,而不会出现任何问题。下面是我的插入查询 INSERT INTO [dbo].[Tests] ([APIID] ,[ActiveID] ,[ABCDataID] ,[ReferenceNumb

我已经看过很多关于这个错误消息的帖子,但似乎没有一篇能涵盖我的问题。错误为:“INSERT语句与外键约束冲突”

我在名为Tests的表中设置了一个外键为Null,因为不是每个记录在另一个名为api的表中都有相应的记录

在SSMS中,我可以插入一条新记录,而不会出现任何问题。下面是我的插入查询

INSERT INTO [dbo].[Tests]
       ([APIID]
       ,[ActiveID]
       ,[ABCDataID]
       ,[ReferenceNumber]
       ,[LocationAddress]
       ,[LocationPostCode]
       ,[Make]
       ,[Model]
       ,[Registration]
       ,[WPC]
       ,[IsA]
       ,[IsS]
       ,[SelfSetDate]
       ,[IsBA]
       ,[UserID]
       ,[ClaimAtFaultEnum]
       ,[ClaimADriverEnum]
       ,[XRepair]
       ,[ManRepair]
       ,[HybridRepair]
       ,[BodyType]
       ,[CustomerName]
       ,[CustomerEmail]
       ,[CustomerMobileNumber]
       ,[IsCancelled]
       ,[CancellationNote])
 VALUES
       (NULL, 
        NULL, 
       NUll,
       '111111111',
       'Waterside',
      'KA18 8EX',
       'Abarth', 
       '320',
       'TIL1607',
       NULL, 
       1,
       0, 
       NULL, 
       0,
       NUll, 
       1, 
       1, 
       0, 
       0, 
       0, 
       'Car',
       'John Smith', 
       'John@TIL1607TestData.com', 
       '07test', 
       0, 
       Null)
GO
下面是我试图使用c#将相同的数据放入数据库的数据,它似乎只有在设置外键时才起作用。我不能将NULL设置为int,因为它是一个int字段,不会接受它,如果我完全忽略它,就会出现上面提到的错误消息

    Test testToAllocate = new Test();

        if (testToAllocate != null)
        {
            int intClaimDriver = -1;
            int intClaimAtFault = -1;
            if (rdoDriverDriver.Checked)
            {
                intClaimDriver = (int)Enums.Test.Driver;
            }
            if (rdoNonDriver.Checked)
            {
                intClaimDriver = (int)Enums.Test.NonDriver;
            }
            if (rdoFaultFault.Checked)
            {
                intClaimAtFault = (int)Enums.Test.Fault;
            }
            if (rdoFaultNonFault.Checked)
            {
                intClaimAtFault = (int)Enums.Test.NonFault;
            }
            if (rdoFaultThirdParty.Checked)
            {
                intClaimAtFault = (int)Enums.Test.ThirdParty;
            }

            ABCData testToABC = db.AudaBridgeClaimDatas.Where(a => a.Registration == txtVehicleRegistration.Text).FirstOrDefault();
            if (testToAllocate != null)
            {
                testToAllocate.ABCDataID = testToABC.ABCDataID;
            }
            else
            {
                testToAllocate.ABCDataID = null;
            }


         //   testToAllocate.APIID = 5; //Needs to be Null
            testToAllocate.ReferenceNumber = "111111111"; 
            testToAllocate.LocationAddress = "Waterside";
            testToAllocate.LocationPostCode = "KA18 8EX";
            testToAllocate.Make = "Abarth";
            testToAllocate.Model = "320";
            testToAllocate.Registration = "TIL1607";
            testToAllocate.IsA = true;
            testToAllocate.IsS = false;
            testToAllocate.IsBA = false;
            testToAllocate.ClaimADriverEnum = 1;
            testToAllocate.ClaimAtFaultEnum = 1;
            testToAllocate.XRepair = false;
            testToAllocate.ManRepair = false;
            testToAllocate.HybridRepair = false;
            testToAllocate.BodyType = "Car";
            testToAllocate.CustomerName = "John Smith";
            testToAllocate.CustomerEmail = "John@TIL1607TestData.com";
            testToAllocate.CustomerMobileNumber = "07test";
            testToAllocate.IsCancelled = false; 

            db.Claims.InsertOnSubmit(testToAllocate);
            db.SubmitChanges();
有人有什么想法吗?就好像VisualStudio没有识别出我对数据库所做的更改,使该字段为空

我不能将NULL设置为int,因为它是一个int字段,并且不会接受它

那么您的模型就不能反映您的数据库。如果数据库有一个可空字段,则需要一个可空int:
int?

如果我完全忽略它,就会出现上面提到的错误消息

因为模型中该字段的数据类型错误,
int
的默认值为
0
。如果目标表中没有相应的记录
0
,则表示外键约束冲突

就好像VisualStudio没有识别出我对数据库所做的更改,使该字段为空

的确如此。如果您正在使用某种工具从数据库生成类,请重新运行该工具。如果没有,请相应地更新您的模型。您有一个可为空的数据库字段和一个不可为空的模型字段

基本上,你的模型上有这样的东西:

public int SomeField { get; set; }
publit int? SomeField { get; set; }
你想要的是这样的:

public int SomeField { get; set; }
publit int? SomeField { get; set; }
然后可以将
SomeField
设置为
null

我不能将NULL设置为int,因为它是一个int字段,并且不会接受它

那么您的模型就不能反映您的数据库。如果数据库有一个可空字段,则需要一个可空int:
int?

如果我完全忽略它,就会出现上面提到的错误消息

因为模型中该字段的数据类型错误,
int
的默认值为
0
。如果目标表中没有相应的记录
0
,则表示外键约束冲突

就好像VisualStudio没有识别出我对数据库所做的更改,使该字段为空

的确如此。如果您正在使用某种工具从数据库生成类,请重新运行该工具。如果没有,请相应地更新您的模型。您有一个可为空的数据库字段和一个不可为空的模型字段

基本上,你的模型上有这样的东西:

public int SomeField { get; set; }
publit int? SomeField { get; set; }
你想要的是这样的:

public int SomeField { get; set; }
publit int? SomeField { get; set; }
然后可以将
SomeField
设置为
null

我已将表中名为Tests的外键设置为Null

我不能将NULL设置为int,因为它是一个int字段,并且不会接受它

将类型设置为可为null的int,而不是
int
。这将允许您将值设置为null

我已将表中名为Tests的外键设置为Null

我不能将NULL设置为int,因为它是一个int字段,并且不会接受它

将类型设置为可为null的int,而不是
int
。这将允许您将值设置为null


请参阅

在db.submitchanges()之前获取生成的sql代码,并与您的工作sql代码进行比较。显示模型-类定义。在db.submitchanges()之前获取生成的sql代码,并与您的工作sql代码进行比较。显示模型-类定义。