C# 自动生成_id时在_id上插入冲突

C# 自动生成_id时在_id上插入冲突,c#,sql-server,C#,Sql Server,我犯了这个错误 The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK__Subjects__fk_sub__6383C8BA". The conflict occurred in database "The Library", table "dbo.Subjects", column '_id'. \u id是标识、唯一和主要的。当内容输入数据库时,应自动生成 我的sql用于创建表 cre

我犯了这个错误

The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK__Subjects__fk_sub__6383C8BA". 
The conflict occurred in database "The Library", table "dbo.Subjects", column '_id'.
\u id
是标识、唯一和主要的。当内容输入数据库时,应自动生成

我的sql用于创建表

create table Subjects
(
    _id integer identity not null,
    unique(_id),
    primary key(_id),
    subject varchar(50) not null,
    [content] varchar(MAX) not null,
    fk_subfolderTo integer not null,
    foreign key(fk_subfolderTo) references Subjects(_id),
    signed varchar(150) not null,
    fk_writer integer not null,
    foreign key(fk_writer) references Users(_id)
)
public static void Create (SqlConnection con, int userId, string subject, string content, string signed, int subfolderTo)
    {
        using (var command = new SqlCommand("Insert into Subjects (subject, [content], fk_subfolderTo, signed, fk_writer) values ('" + subject + "', '" + content + "', " + subfolderTo + ", '" + signed + "', " + userId + ")", con))
        {
            command.ExecuteNonQuery();
        }
    }
以及用于插入到表中的C#代码

create table Subjects
(
    _id integer identity not null,
    unique(_id),
    primary key(_id),
    subject varchar(50) not null,
    [content] varchar(MAX) not null,
    fk_subfolderTo integer not null,
    foreign key(fk_subfolderTo) references Subjects(_id),
    signed varchar(150) not null,
    fk_writer integer not null,
    foreign key(fk_writer) references Users(_id)
)
public static void Create (SqlConnection con, int userId, string subject, string content, string signed, int subfolderTo)
    {
        using (var command = new SqlCommand("Insert into Subjects (subject, [content], fk_subfolderTo, signed, fk_writer) values ('" + subject + "', '" + content + "', " + subfolderTo + ", '" + signed + "', " + userId + ")", con))
        {
            command.ExecuteNonQuery();
        }
    }

im使用MSSQL。

正在生成ID。问题是,作为子文件夹ID传递的内容不在现有数据集中

我还建议将fk_子文件夹设置为可空,否则您将被迫将主题设置为自身的子主题


此外,在经历SQL注入噩梦之前,您应该使用,而不是通过字符串连接来构建SQL。

为什么使用同一个表作为引用[外键(fk_子文件夹)引用主题(_id)]?因为您可以有子主题