I want to insert data three relation table but I can't do this :/
我想插入数据3关系表,但是我做不到:/
here is the code
这是代码
hali_sahaEntities con = new hali_sahaEntities();
Users user = new Users();
Emails email = new Emails();
Phones phone = new Phones();
user.Name = textBox1.Text;
user.Surname = textBox7.Text;
user.IdentityNumber = Convert.ToInt32( textBox2.Text);
user.Adress = textBox5.Text;
user.Comment = textBox6.Text;
email.TCKN = Convert.ToInt32( textBox2.Text);
email.Email = textBox4.Text;
phone.TCKN = Convert.ToInt32(textBox2.Text);
phone.Phone = textBox3.Text;
con.Users.Add(user);
con.Emails.Add(email);
con.Phones.Add(phone);
con.SaveChanges();
I can insert data Users Table but Emails and Phones tables can not insert data?
我可以插入数据用户表,但是电子邮件和电话表不能插入数据?
在这里我的表
3 个解决方案
#1
1
In this case you will be gave this error
在这种情况下,你会得到这个错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Emails_Users". The conflict occurred in database "****", table "dbo.Users", column 'IdentityNumber'. The statement has been terminated.
插入语句与外键约束“FK_Emails_Users”冲突。冲突发生在数据库“****”,表“dbo”中。用户”,列“IdentityNumber”。声明已被终止。
so you can fix it by this change:
你可以通过这个改变来修正
con.Users.Add(user);
con.SaveChanges();//*Because of the relationship*
con.Emails.Add(email);
con.Phones.Add(phone);
con.SaveChanges();
#2
1
You can use
您可以使用
hali_sahaEntities con = new hali_sahaEntities();
Users user = new Users();
user.Name = textBox1.Text;
user.Surname = textBox7.Text;
user.IdentityNumber = Convert.ToInt32( textBox2.Text);
user.Adress = textBox5.Text;
user.Comment = textBox6.Text;
Emails email = new Emails();
email.TCKN = Convert.ToInt32( textBox2.Text);
email.Email = textBox4.Text;
user.Emails.Add(email);//Emails is virtual proparty
Phones phone = new Phones();
phone.TCKN = Convert.ToInt32(textBox2.Text);
phone.Phone = textBox3.Text;
user.Phones.Add(phone);//Phones is virtual proparty
con.Users.Add(user);
con.SaveChanges();
#1
1
In this case you will be gave this error
在这种情况下,你会得到这个错误
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Emails_Users". The conflict occurred in database "****", table "dbo.Users", column 'IdentityNumber'. The statement has been terminated.
插入语句与外键约束“FK_Emails_Users”冲突。冲突发生在数据库“****”,表“dbo”中。用户”,列“IdentityNumber”。声明已被终止。
so you can fix it by this change:
你可以通过这个改变来修正
con.Users.Add(user);
con.SaveChanges();//*Because of the relationship*
con.Emails.Add(email);
con.Phones.Add(phone);
con.SaveChanges();
#2
1
You can use
您可以使用
hali_sahaEntities con = new hali_sahaEntities();
Users user = new Users();
user.Name = textBox1.Text;
user.Surname = textBox7.Text;
user.IdentityNumber = Convert.ToInt32( textBox2.Text);
user.Adress = textBox5.Text;
user.Comment = textBox6.Text;
Emails email = new Emails();
email.TCKN = Convert.ToInt32( textBox2.Text);
email.Email = textBox4.Text;
user.Emails.Add(email);//Emails is virtual proparty
Phones phone = new Phones();
phone.TCKN = Convert.ToInt32(textBox2.Text);
phone.Phone = textBox3.Text;
user.Phones.Add(phone);//Phones is virtual proparty
con.Users.Add(user);
con.SaveChanges();