My SQL Server database contains several views which are essentially filtered (WHERE) / ordered (ORDER BY) versions of some of the tables i.e. no joins.
An example would be my Downloads table, which has a view such as DownloadsOnlineOnly e.g.
SELECT * FROM Downloads WHERE Live = 1 ORDER BY FolderName ASC;
I can drag and drop these views onto my LINQ to SQL DataContext, and use LINQ to query these views and retrieve data fine…
But the foreign key relationships are not being picked up in my DataContext, so I’ve lost the ability to:
Dim dl as DownloadOnlineOnly = (From r in db.DownloadOnlineOnlies Select r).First
Dim fol as Folder = dl.Folder ' Syntax error as there is no association between
' DownloadOnlineOnly and Folder in my DataContext.
I understand why this is happening, but how can I get around this:
- Without manually adding associations in my DataContext?
- Can I change the type of the Entity like changing the return type of an SP on the DataContext?
- Can I cast or convert between a DownloadOnlineOnly object and Download object?
- Or some other method?
Apologies if this is a trivial problem, I am relatively new to LINQ.
P.S. I am developing with SQL Server 2008 R2 databases.
You have to hook them up manually. Nothing you can do about it. Just be thankful you’re not using EF, because EF’s support of views is even worse. It assumes any non-null field is part of a Composite primary key.