违反主键约束pk_T_registration,无法在对象dbo.T_registration错误中插入重复键 [英] violation of primary key constraint pk_T_registration, cannot insert duplicate key in the object dbo.T_registration error

查看:225
本文介绍了违反主键约束pk_T_registration,无法在对象dbo.T_registration错误中插入重复键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个注册表,其中pateintid作为主键,带有自动增量1.

i我使用linq提交我的deatails,如名称,性别....问题是,只有一个记录插入当我尝试插入上面的第二条记录时,表格开始以0为准,错误即将来临.......... plz给出一些想法



这里是我的代码



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;



公共部分类PatientRegistration:System.Web.UI.Page

{



ADO objado = new ADO();

DataClassesDataContext objlinq = new DataClassesDataContext();

T_patient_registration objreg = new T_patient_registrati on();











protected void Page_Load(object sender,EventArgs e)

{



}



protected void Button1_Click(object sender,EventArgs e)

{

objreg.name = txtpatientname.Text;

objreg.father_mothername = txtguardianname.Text;

objreg.gender = drpgender.SelectedItem.ToString();

objreg.dateofbirth = txtdob.Text;

objreg .disease = txtdisease.Text;

objreg.age = Convert.ToInt32(txtage.Text);

objreg.emailid = txtemail.Text;

objreg.currentaddress = txtcaddress.Text;

objreg.city = drpcity.SelectedItem.ToString();

objreg.contactnumber = Convert.ToInt64(txtcontact.Text) ;

objreg.permanentaddress = txtpaddress.Text;

objreg.spe cialist = drpspecialist.SelectedItem.ToString();

objreg.occupation = drpoccupation.SelectedItem.ToString();

objlinq.T_patient_registrations.InsertOnSubmit(objreg);

objlinq.SubmitChanges();

Label1.Text =数据已成功插入;



}



}

i created one registyration table with pateintid as primary key with with autoincrement 1.
i am using linq to submit my deatails like name,gender....the problem was , only one record is inserting in the table starts pateintid with 0 when i try to insert second record above error is coming..........plz give some idea

here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class PatientRegistration : System.Web.UI.Page
{

ADO objado = new ADO();
DataClassesDataContext objlinq = new DataClassesDataContext();
T_patient_registration objreg = new T_patient_registration();





protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
objreg.name = txtpatientname.Text;
objreg.father_mothername = txtguardianname.Text;
objreg.gender = drpgender.SelectedItem.ToString();
objreg.dateofbirth = txtdob.Text;
objreg.disease = txtdisease.Text;
objreg.age = Convert.ToInt32(txtage.Text);
objreg.emailid = txtemail.Text;
objreg.currentaddress = txtcaddress.Text;
objreg.city = drpcity.SelectedItem.ToString();
objreg.contactnumber = Convert.ToInt64(txtcontact.Text);
objreg.permanentaddress = txtpaddress.Text;
objreg.specialist = drpspecialist.SelectedItem.ToString();
objreg.occupation = drpoccupation.SelectedItem.ToString();
objlinq.T_patient_registrations.InsertOnSubmit(objreg);
objlinq.SubmitChanges();
Label1.Text = "Data Inserted Successfully";

}

}

推荐答案

objlinq.T_patient_registrations.InsertOnSubmit(objreg);



不确定这是什么。我将对象添加到数据库上的对象集合,然后保存更改。你没有设置id,所以它应该工作,我刷新EF DB定义,看看那里发生了什么。
objlinq.T_patient_registrations.InsertOnSubmit(objreg);

Not sure what this is. I'd add the object to the collection of objects on the DB, then save changes. You're not setting the id, so it should work, I'd refresh the EF DB definition and see what happens from there.


这篇关于违反主键约束pk_T_registration,无法在对象dbo.T_registration错误中插入重复键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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