首页 文章

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

提问于
浏览
0
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

    在您的更新声明中:

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

    您正在选择 Id == a 使用 FirstOrDefault 的记录,并且假设EntityFramework可以't update this record, I' m假设没有记录存在 Id == a 所以您需要处理查询中的空返回值:

    var updatebook = db.k_Books.FirstOrDefault(w => w.ID == a);
    
    if (updatebook != null)
    {
        updatebook.Kitap_statusId = 2;
        db.SaveChanges();
    }
    

    在这种情况下,如果查询返回了一条记录,它将更新 Kitap_statusId ,否则它将不会执行任何操作 .

    更新

    每个OP对此问题的评论:

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

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

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

    db.k_Rentedbooks.Add(rent);
    

    为此执行 SaveChanges() 时,它会自动使用新的Id更新实体,因此在该函数中降低,然后可以使用 rent 的Id值将新记录添加到第2个表 .

    作为一个粗略的例子:

    var Book = new Book{ statusId = rent.Id };
    db.SecondTable.Add(Book);
    db.SaveChanges();
    

相关问题