更新数据时SQL异常违反主键约束

时间:2017-07-18 08:48:56

标签: c# entity-framework linq

private void FormRentBook_Load(object sender, EventArgs e)
    {
        librarydb sorgu = new library();
        var book = from booklist in query.k_Books
                    join isrent in query.k_Bookstatus
                    on booklist.Book_statusId equals isrent.k_typeid
                    join booktype in query.k_BookType
                    on booklist.book_type equals booktype.ID
                    select new
                    {

                       booklist.Book_Name,
                       booklist.Book_Author,
                       booktype.Book_type,
                       booklist.Book_Page,
                       booklist.ID,
                       isrent.k_typecomment,
                    };
        bookscreen.DataSource = book.ToList();
        bookscreen.Columns[0].HeaderText = "Book Name";
        bookscreen.Columns[1].HeaderText = "bookscreen Author";
        bookscreen.Columns[2].HeaderText = "Book Type";
        bookscreen.Columns[3].HeaderText = "Page Number";
        bookscreen.Columns[4].Visible = false;
        bookscreen.Columns[5].HeaderText = "Book Status";
        bookscreen.Show();
        label6.Text = DateTime.Now.ToString();
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

    }
        public int a;
    private void bookscreen_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        label1.Text = bookscreen.CurrentRow.Cells[0].Value.ToString();
        label2.Text = bookscreen.CurrentRow.Cells[1].Value.ToString();
        a =int.Parse( bookscreen.CurrentRow.Cells[4].Value.ToString());
        label3.Text = bookscreen.CurrentRow.Cells[5].Value.ToString();
    }

k_Rentedbooks rent = new k_Rentedbooks();
rent.renter_id = Login.k_id;
rent.renter_id = int.Parse(bookscreen.CurrentRow.Cells[4].Value.ToString());
rent.rent_date = DateTime.Now;

DateTime return = DateTime.Now;
int day;
day = Convert.ToInt32(comboBox1.SelectedItem.ToString());

rent.returndate = return.AddDays(day);
db.k_Rentedbooks.Add(rent);

var updatebook = db.k_Books.Where(w => w.ID ==a).FirstOrDefault();
updatebook.Kitap_statusId = 2;
db.SaveChanges();

我需要将数据添加到k_KiralananKitaplar并更新名为Kitap_DurumId = 2的行但我只能添加数据或更新我不能一次性db.SaveChanges给我错误

以下是数据样本:

  • Kitap_Adi =图书名称,
  • Kitap_Yazar = book_author,
  • Kitap_Tur =图书类型,
  • Kitap_Sayfa =书页,
  • Kitap_DurumId =图书状态

enter image description here

错误消息是

  

SqlException:违反PRIMARY KEY约束'PK_k_KiralananKitaplar'。无法在对象'dbo.k_KiralananKitaplar'中插入重复键。重复键值为(0)。

1 个答案:

答案 0 :(得分:0)

在您的更新声明中:

var updatebook = db.k_Books.Where(w => w.ID ==a).FirstOrDefault();
updatebook.Kitap_statusId = 2;
db.SaveChanges();

您选择Id == a使用FirstOrDefault的记录,并且鉴于EntityFramework无法更新此记录,我将假设其中没有记录{ {1}}因此您需要处理查询中的空返回值:

Id == a

在这种情况下,如果查询返回了记录,则会更新var updatebook = db.k_Books.FirstOrDefault(w => w.ID == a); if (updatebook != null) { updatebook.Kitap_statusId = 2; db.SaveChanges(); } ,否则,它将无法执行任何操作。

更新

每个OP对此问题的评论:

  

我解决了问题,但现在我需要找到当我在表中插入新数据时其他表插入到同一个表中我该怎么做

您只需要获取新插入实体的Id值,并使用它将记录插入第二个表。

因此,在您的代码中,您将插入第一个项目:

Kitap_statusId

为此执行db.k_Rentedbooks.Add(rent); 时,它会自动使用新ID更新实体,因此在该功能中降低,然后您可以使用Id值将新记录添加到第2个表SaveChanges()

粗略的例子:

rent