Sql – LinqToSql and Views don’t have foreign keys/associations

associationsforeign-keyslinq-to-sqlnetview

I've got some tables in my Linq2Sql designer (in Visual Studio 2008). Works great.

Now, I just tried dropping in a View onto the designer. It dropped on there ok, but there's NO foreign keys/associations.

hmm. Ok, so then I tried to manually add an association between the View and a parent table. That worked .. but when i try to access the parent table programatically, it's not listed there / doesn't compile.

er, wtf?!

Has anyone else had this problem? Are there some special work arounds?

I'm using a View because it's an Indexed View.

Cheers!

UPDATE

Found some related links…

  1. LINQ to SQL – Problem with 1-to-1 association
  2. LINQ to SQL doesn't generate code for view associations
  3. Linq TO SQL 1 to many relations between Table and view

Best Answer

The answer is that a View doesn't have any foreign key's with it's meta data. So when Visual Studio tries to generate any associations, etc, it can't. As such, you need to manually add your own associations in.