SQL Server System.Data.SqlClient.SqlException:'INSERT语句与FOREIGN KEY约束冲突

icomxhvb  于 2023-01-16  发布在  其他
关注(0)|答案(2)|浏览(221)

我在调试过程中仍然收到这个错误。我不知道该怎么做,因为我已经为Person klass添加了AddressID。
救命啊!
INSERT语句与FOREIGN KEY约束"FK_Person_ToAddress"冲突。冲突发生在数据库"DirectoryDatabase"、表"dbo.Address"、列"AddressID"中
抛出此错误的函数为:

public void CreatePersonDB(ref Person person)
    {
        string CreatePerson =
            @"INSERT INTO [Person] (FirstName, MiddleName, LastName, AddressID)
                                    OUTPUT INSERTED.PersonID  
                                    VALUES (@FName, @MName, @LName, @AID)";

        using (SqlCommand cmd = new SqlCommand(CreatePerson, OpenConnection))
        {
            // Get your parameters ready                    
            cmd.Parameters.AddWithValue("@FName", person.FirstName);
            cmd.Parameters.AddWithValue("@MName", person.MiddleName);
            cmd.Parameters.AddWithValue("@LName", person.LastName);
            cmd.Parameters.AddWithValue("@AID", person.PrimaryAddress.AddressID);

             try
            {
                person.PersonID = (int)cmd.ExecuteScalar(); //Returns the identity of the new tuple/record}
            }
            catch
            {
                Console.WriteLine("Adresse ID doesn't exist, do you want to add it? [y/n]");
                ConsoleKeyInfo input = Console.ReadKey();

                if (input.Key == ConsoleKey.Y)
                {
                    //create an insert query to the dbo.Adresse the same way you did with the dbo.person.
                    CreateAddressDB();
                }
        }
    }

Person & Address的数据库sql代码如下所示(编辑后):

CREATE TABLE Address (
AddressID      BIGINT IDENTITY(1,1) NOT NULL,
StreetName     NVARCHAR(MAX) NOT NULL,
HouseNumber    NVARCHAR(MAX) NOT NULL,

CityID         BIGINT NOT NULL,

[个人ID] NCHAR(10)不为空,[主地址] INT不为空,约束pk_地址主键群集(地址ID),约束fk_地址外键(城市ID)引用城市(城市ID)删除时无操作更新时无操作)
以下是地址表:

CREATE TABLE Person (
PersonID       BIGINT IDENTITY(1,1) NOT NULL,
FirstName      VARCHAR(50) NOT NULL,
MiddleName     NVARCHAR(50) NOT NULL,
LastName       NVARCHAR(50) NOT NULL,

AddressID      BIGINT NOT NULL,

约束pk_人员主键群集(人员ID),约束fk_人员外键(地址ID)引用地址(地址ID))

jm2pwxwz

jm2pwxwz1#

表dbo.Address中不存在具有您的person.PrimaryAddress.AddressID值的记录

4dbbbstv

4dbbbstv2#

您正在尝试将AdresseID插入到person表中,而Adresse表中不存在该表。
试试这个:

public void CreatePersonDB(ref Person person)
{
    string CreatePerson =
        @"INSERT INTO [Person] (FirstName, MiddleName, LastName, AddressID)
                                OUTPUT INSERTED.PersonID  
                                VALUES (@FName, @MName, @LName, @AID)";

    using (SqlCommand cmd = new SqlCommand(CreatePerson, OpenConnection))
    {
        // Get your parameters ready                    
        cmd.Parameters.AddWithValue("@FName", person.FirstName);
        cmd.Parameters.AddWithValue("@MName", person.MiddleName);
        cmd.Parameters.AddWithValue("@LName", person.LastName);
        cmd.Parameters.AddWithValue("@AID", person.PrimaryAddress.AddressID);

        try()
        {
            person.PersonID = (int)cmd.ExecuteScalar();   // Returns the identity of the new tuple/record}
        catch()
        {
             DialogResult dialogResult = MessageBox.Show("Adresse ID doesn't exist, do you want to add it?", "Alerte",MessageBoxButtons.YesNo);

             if(dialogResult == DialogResult.Yes)
             {
                 // create an insert query to the dbo.Adresse the same way you did with the dbo.person.
             }
        }
    }
}

相关问题