【问题标题】:Can't INSERT new Record due to Foreign Key Issue由于外键问题,无法插入新记录
【发布时间】:2018-11-28 08:24:14
【问题描述】:

我看过很多关于这个特定错误消息的帖子,但似乎没有一个能涵盖我的问题。错误是:'INSERT 语句与 FOREIGN KEY 约束冲突'

我已在名为 Tests 的表中将外键设置为 Null,因为并非每条记录都会在另一个名为 APIS 的表中具有相应的记录。

在 SSMS 中,我可以毫无问题地插入新记录,下面是我的 INSERT 查询。

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 字段并且不会接受它,如果我完全忽略它,它会出现上面提到的错误消息。

    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();

有人有什么想法吗?就好像 Visual Studio 没有识别出我对数据库所做的更改以使该字段为空。

【问题讨论】:

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

标签: c# sql linq tsql


【解决方案1】:

我在名为 Tests 的表中设置了一个外键为 Null

我不能输入 NULL,因为它是一个 int 字段并且不会接受它

而不是int,创建类型int?,它是一个可以为空的int。这将允许您将值设置为 null。

MSDN

【讨论】:

    【解决方案2】:

    我不能输入 NULL,因为它是一个 int 字段并且不会接受它

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

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

    因为模型对该字段的数据类型错误,而int 的默认值为0。如果目标表中没有对应的记录0,那就是外键约束违规。

    几乎就像 Visual Studio 没有识别出我对数据库所做的更改以使该字段为空。

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

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

    public int SomeField { get; set; }
    

    你想要的是这样的:

    publit int? SomeField { get; set; }
    

    然后你可以将SomeField设置为null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多