无法使用LINQ在具有复合主键的表中插入

时间:2018-04-29 11:21:51

标签: c#-4.0 linq-to-sql ado.net composite-primary-key

在此网站上添加SqlConnection部分有人建议,但仍然会出现异常

  

当标识插入为OFF时,无法为标识列插入显式值

on dv.SubmitChanges();

复合主键由stidbookid组成。

另请告诉我如何在不连接文本框的值的情况下进行此查询,例如bname.Text

using (SqlConnection conn = new SqlConnection(cs))
{
    conn.Open();

    using (DataClasses1DataContext dv = new DataClasses1DataContext())
    {
        var iq = (from b in dv.Books
                  where SqlMethods.Like(b.name, "%" + bname.Text + "%")
                  select b.Id).FirstOrDefault();

        IssueInfo info = new IssueInfo()
                             {
                                 stid = Convert.ToInt32(rollno.Text),
                                 due_date = Convert.ToDateTime(dateTimePicker1.Text),
                                 bookid = Convert.ToInt32(iq)
                             };

        dv.ExecuteCommand("SET IDENTITY_INSERT IssueInfo ON");
        dv.IssueInfos.InsertOnSubmit(info);
        dv.SubmitChanges();
        dv.ExecuteCommand("SET IDENTITY_INSERT IssueInfo OFF");
    }
}

0 个答案:

没有答案