2

I should know this but I expect it to be a simple answer...

This is my simple database so far for this question:

alt text

Am I supposed to insert the Id from Contact and Phone into Contact_Phone when I insert a record into Contact and Phone tables - or can this be done automatically?

I am using SQL Server 2008 Express.

3 Answers3

3

You have to insert it yourself in SQL, but it depends entirely on how you're accessing the database.

The Entity Framework should be able to handle this for you automatically.

Bennor McCarthy
  • 11,415
  • 1
  • 49
  • 51
  • Playing with NHibernate for the first time.. not sure if it does automatically or not. –  Aug 06 '10 at 20:34
  • This should help: http://codebetter.com/blogs/peter.van.ooijen/archive/2008/05/29/nhibernate-many-to-many-collections-or-mapping-is-not-one-table-one-class.aspx – Bennor McCarthy Aug 06 '10 at 20:41
2

You need to insert it. And since the relationships are defined, you will need to insert it into the Contact and Phone tables first.

David
  • 72,686
  • 18
  • 132
  • 173
1

How could the database know which contacts and phones go together unless you insert them? Unfortunately in SQL Server you can't update more than one table at once - a limitation shared by most if not all SQL DBMSs.

nvogel
  • 24,981
  • 1
  • 44
  • 82
  • Well I figured if there was a foreign key constraint then it would know the many-to-many relationship that is already graphed –  Aug 06 '10 at 22:01
  • The database has no way of knowing which row is related to which other row unless you tell it. Possibly your application does know because the application uses an object model with pointers to associate one item with another. There are no pointers in the database however. – nvogel Aug 06 '10 at 22:29
  • A foreign key is a reference, but it is not a pointer. No matter. With pointers, you would still have to tell the data structure which Contact owns which Phone. – Walter Mitty Aug 07 '10 at 08:26