私はこのエラーが発生しています
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'.
は_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)
)
テーブルに挿入するための C# コード。
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();
}
}
MSSQLを使用しています。