The local database has a User PersonName with the fields PersonName , PersonPass and ID(pk) . The ID uniqueidentifier data type and a default value ( newid() ). When I just add data to the table, the key is really unique, but when I use insert through a form using linq to sql insert in the database, something like 00000-000000 in the key field and when adding a new user, an error occurs because the second user in the key field the same zeros. What is causing this and how to fix it?

 private void buttonRunReg_Click(object sender, EventArgs e) { using (DataClasses1DataContext db = new DataClasses1DataContext()) { User sequence = (from user in db.User where user.PersonName == textBox1.Text select user).SingleOrDefault(); if (sequence==null) { if ((textBox1.Text != "") && textBox2.Text.Equals(textBox3.Text)) { User userTable = new User(); userTable.PersonName = textBox1.Text; userTable.PersonPass = textBox2.Text; db.User.InsertOnSubmit(userTable); try { db.SubmitChanges(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } labelExForm2.Text = "регистрация прошла успешно"; labelExForm2.Visible = true; } else if (!textBox2.Text.Equals(textBox3.Text)) { labelExForm2.Text = "пароли не совпадают"; labelExForm2.Visible = true; } else { labelExForm2.Text = "Птушка не клюет зерно"; labelExForm2.Visible = true; } } else { labelExForm2.Text = "Имя "+textBox1.Text+" уже занято. попробуйте другое."; labelExForm2.Visible = true; } } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

In the Linq to Sql designer, set the ID column to true in the Auto Generated Value parameter.

enter image description here

More details:

https://msdn.microsoft.com/ru-ru/library/bb386929(v=vs.110).aspx (block "Warning explicitly setting values ​​created by the database in Insert or Update")

https://stackoverflow.com/questions/753003/linq-to-sql-insert-sequential-guid

https://stackoverflow.com/questions/1182946/auto-generate-in-linq-to-sql

  • Thank you very much. - Anna992
  • @ Anna992 If the answer helped you, please mark it as correct (a daw opposite the selected answer). - Ella Svetlaya