There are two tables. They need to enter data from the TextBox :

 class PersonData { public string FirstName { get; set; } public string LastName { get; set; } public string WorkPhone { get; set; } public string HomePhone { get; set; } Person person = new Person(); Phones phones = new Phones(); public PersonData(string firstName, string lastName, string workPhone, string homePhone) { FirstName = firstName; LastName = lastName; WorkPhone = workPhone; HomePhone = homePhone; } public void InsertData() { NoteBookDataContext noteBook = new NoteBookDataContext(); Person person = new Person(); Phones phones = new Phones(); person.FirstName = FirstName; person.LastName = LastName; phones.HomePhone = HomePhone; phones.WorkPhone = WorkPhone; noteBook.Person.InsertOnSubmit(person); noteBook.Phones.InsertOnSubmit(phones); noteBook.SubmitChanges(); } } 

On the button hung handler:

 private void Button_Click(object sender, RoutedEventArgs e) { PersonData personData = new PersonData(TbFirstname.Text, TbLastName.Text, TbWorkPhone.Text, TbHomePhone.Text); personData.InsertData(); } 

It throws an exception like this:

Additional information: INSERT statement conflict with FOREIGN KEY constraint "FK_Phones_Person". The conflict occurred in the database "NoteBookTest", table "dbo.Person", column 'id'.

  • Additional information: INSERT statement conflict with FOREIGN KEY constraint "FK_Phones_Person". The conflict occurred in the database "NoteBookTest", table "dbo.Person", column 'id'. - Ildarik07
  • throws such an exception - Ildarik07
  • I assume the error is due to the fact that you have inconsistency of data. in fact, the entry in the database gets after the submission. those. First, submit to person, then insert into phones. and that everything would be correct, you need to use transactions - Bald

1 answer 1

So where in your model of communication between tables? Add fields for communication.

How to make linked tables, written in any EF tutorial (do you use it?)

  • not! I am using Linq to SQL - Ildarik07
  • Oh, right. But everything is very similar there. Just add an association between entities in the model designer - and set it when inserting. - Pavel Mayorov
  • Duck I added like! NoteBook.dbml here is this file! do you mean it ??? then NoteBookDataContext noteBook = new NoteBookDataContext (); I couldn’t have done so if it wouldn’t have happened - Ildarik07
  • one
    No, I mean this: person.Phones.Add(phone) - or this: phone.Person = person . - Pavel Mayorov
  • Such a relationship between entities in EF is called Relation (relation) - and in Linq2Sql - Assotiation. - Pavel Mayorov