I got asked about doing this recently, and needed somewhere to point the individual to.
Presently, unfortunately, Linq to SQL Compact Edition is unsupported. Luckily though, it is quite easy to still perform Linq queries against SQL compact using the command line utility SqlMetal. SQL Compact Edition is very handy for single user applications or demos where you want to retain a very small application imprint, but not lose functionality. All that is required is that you have the object relational mapping (ORM) file that contains the Linq to SQL classes. This in Visual Studio is the .dbml file that is generated when you elect LinqToSQLClasses in the data menu.
Open up the Visual Studio command prompt by going to
You should have the following;
C:\Program Files\Microsoft Visual Studio 9.0\VC>
Enter ‘SqlMetal’ in the command prompt and take time to explore all the options available to you.
I simply want to create a .dbml file from my database which I have in my c:\ drive. You will want to point this to wherever your .sdf file is.
Enter the following path into the Visual Studio command prompt
SqlMetal /dbml:c:\Users\MyName\Documents\Northwind.dbml "C:\Users\MyName\Documents\Visual Studio 2008\Projects\Windows Forms\Code\Northwind\Northwind Database\Northwind.sdf"
Where the above is the format;
SqlMetal /dbml:northwind.dbml northwind.sdf
Note that in vista you must specify the path through the ‘Users folder’. Failure to do this will result in a file access error.
As you can see my .dbml file was copied to my documents folder. I then copied this into my project. .To access the file I reference it in the constructor of the form thus;