Over a million developers have joined DZone.

Generating a LINQ to SQL DataContext for VS Express for Windows Phone

DZone's Guide to

Generating a LINQ to SQL DataContext for VS Express for Windows Phone

· Mobile Zone ·
Free Resource

As you may know, you can generate a Windows Phone DataContext based on an existing database for use with Windows Phone Mango “Local Database”, as described here and here. But this feature is only available if you own a paid Visual Studio edition (Pro or higher), as the Express editions do not support add-ins.

But users of Visual Studio 2010 Express for Windows Phone can now also take advantage of all the productivity benefits of the “Database First” approach to Local Database development on Windows Phone.

Provided you have a SQL Server Compact Database ready, designed according to the guidelines here, and possibly created by using a tool, you can follow this simple procedure to generate and add the DataContext to your Windows Phone solution.

Anyone using the Express tools will notice that SQLMetal (which is used behind the scenes to create the DataContext file) is not included. You can get it from the Microsoft SDK bundle http://www.microsoft.com/download/en/details.aspx?id=8279. You only need to choose the following options to get the required files.


Then go to the download page for my ExportSqlCe tool, and download the latest version of the Exportsqlce.exe command line utility.

You can now generate a DataContext file from the command line, as documented here.

In this example, I will generate a DataContext based on the Chinook sample database.

exportsqlce wpdc "Data Source=C:\projects\Chinook\Chinook.sdf" C:\temp\Chinook.cs

Notice that the format of the database must be SQL Server Compact 3.5.

In Visual Studio, open your Windows Mango project, and from Solution Explorer, select Add, Existing Item, and browse to the file the was just created.


Remember to add a reference to System.Data.Linq to your project.


You can now start using the generated DataContext in your project!

<span style="color: blue">using </span>(ChinookContext db = <span style="color: blue">new </span>ChinookContext(ChinookContext.ConnectionString))
    db.LogDebug = <span style="color: blue">true</span>;

    <span style="color: blue">var </span>artist = db.Artists.FirstOrDefault();
    artist.Name = <span style="color: #2b91af">Guid</span>.NewGuid().ToString();


Happy coding…

Source: http://erikej.blogspot.com/2012/01/generating-linq-to-sql-datacontext-with.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+ErikejBlogsAboutSqlCompactnetAndRelatedStuff+%28Everything+SQL+Server+Compact%29&utm_content=Google+Reader


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}