user1477388
user1477388

Reputation: 21440

How to Create Relationship in VS 2010?

As the following screenshot demonstrates, I am trying to create a relationship between NOTES.PARENT_ID and FILES.ID. How can I accomplish this? I have looked at many references such as http://social.msdn.microsoft.com/Forums/en-US/silverlightwinphone/thread/0043364b-ba03-482c-9fd5-12ae2c7692c3/ and http://msdn.microsoft.com/en-us/library/bb384511.aspx but haven't been able to figure this out.

Thanks for your help.

enter image description here

Edit:

enter image description here

Upvotes: 1

Views: 1241

Answers (3)

Oliver
Oliver

Reputation: 9002

Using the Linq to SQL designer, you can right click in the designer and click Add > Association.

You would then choose the fields from each table to be involved in the relationship.

In your case, parent class would be FILE and child class would be NOTE.

Adding the relationship this way only applies the relationship to the Linq to SQL context. To add the relationship in the database itself, you will have to use the table designer, which is accessible by connecting to the server in visual studio's server explorer.

If the relationship was first added to the database, the Linq to SQL designer would pick up on this and automatically add the relationship.

Upvotes: 2

paul
paul

Reputation: 22001

If at all possible, I would define this relationship in the database.

By creating a foreign key relationship between the two tables in your database:

(a) The association you ask for in your question would be added automatically

and

(b) The database will actually enforce that all NOTE rows, must be associated with an existing FILE

Upvotes: 1

Leo Chapiro
Leo Chapiro

Reputation: 13994

This is explained here: http://msdn.microsoft.com/en-us/library/ms171915.aspx

You have to create the primary keys of both tables first!

  • Creating the Relationship Between the Tables

    The relationship is created between the common column from each table — in this case, the CustomerID column. To configure a new relationship between the Customers and Orders tables:

    Drag a Relation object from the DataSet tab of the Toolbox onto the Orders table.

    The Relation dialog box opens.

    In the Parent Table box, select Customers.

    In the Child Table box, select Orders.

    In the Columns box, set Key Columns to CustomerID.

    In the Columns box, set Foreign Key Columns to CustomerID.

    Click OK to create the relationship; a relation line appears on the designer between the two tables.

    On the Data menu, choose Show Relation Labels.

Upvotes: 1

Related Questions