LINQ to SQL FAQ – Associations to Views

Here are a few things I learned while developing for LINQ to SQL.

Associations to Views

Views can be added to the designed and manual associations can be added between them and other tables/views. However, the association will not create a parent/child property in the code unless you modify the view properties to define "primary keys".

Example: using NorthWind, create a blank LINQ-to-SQL model. Drag the Orders table and the Order_Subtotals view onto the designer, and create an association between "OrderID" on each.

Now compile – you should see a warning from the compiler that Order_Subtotal does not have primary keys and therefore the relationship could not be coded.

Solution: in Order_Subtotal object, set the IsPrimaryKey = true for column "OrderID". This will enabled the relationship to be created.

Advertisements

3 thoughts on “LINQ to SQL FAQ – Associations to Views

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s