db中的外键冲突 [英] Foreign key Conflict in db

查看:86
本文介绍了db中的外键冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码行想要在db中提交值,但是当我单击打开"按钮时出现以下错误.我在这些表中将两个表设置为EmpId设置为主键,而在其他表中将id设置为主键两者都是通过FK关系关联的.哪里有问题,我应该在哪里进行更改.
谢谢&问候.

I have following line of code want to submit value in the db but when I click On button but I get a following error.I have Made two tables in those tables EmpId Set to Primary Key And Id in other table set primary key and both are related by FK relationship.Where is problem where should I make changes.
Thanks & Regards.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CheckInCheckOut_Employee". The conflict occurred in database "EmployeeRecord", table "dbo.Employee", column 'empId'.
The statement has been terminated.


 protected void imgbtnUpadteDB(object sender, ImageClickEventArgs e)
        {
            DBClassesDataContext db = new DBClassesDataContext();
            CheckInCheckOut chk = new CheckInCheckOut();
            Employee em = new Employee();
            chk.checkTime = System.DateTime.Now;
            if (chk.checkTime != null)
            {
                chk.checkTime = DateTime.Now;
            }
            else
            {
                chk.checkoutTime = DateTime.Now;
            }
            
            empId = 1;
          
            em.name = txtCheckIn.Text;
            db.CheckInCheckOuts.InsertOnSubmit(chk);
            db.Employees.InsertOnSubmit(em);
            db.SubmitChanges();
            Response.Redirect("Default.aspx");
}

推荐答案

db.CheckInCheckOuts.InsertOnSubmit(chk);
db.Employees.InsertOnSubmit(em);



 1st statement should be
 db.Employees.InsertOnSubmit(em);
 Then 
 2nd statement should be
 db.CheckInCheckOuts.InsertOnSubmit(chk);


这篇关于db中的外键冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆