Windows Phone 7 "Mango" Visual Studio Custom Tool

To use the custom tool, select the SQL Compact Edition database file in the solution explorer and open the properties window. The custom tool is called SQLCEMangoCodeGenerator. Write this in the Custom Tool field in the Properties Window of an SDF file included in your project, this should create a code-behind file for the generated data access code


The Generated Code

A LINQ to SQL Data Context class is generated that contains every table in the database and 2 constructor methods. The default constructor sets the connection to use "Data Source=isostore:/SDF File" while the other constructor accepts a connection string as an argument. The constructor checks if the database exists using the connection string, the database is created if it does not exist. The database is created by calling the CreateDatabase() method of the DataContext base class.

Using the Generated Windows Phone 7 "Mango" Data Context Code

using (var dataContext = new EntityDataContext())
{
    var contact = new Contact
    {
	Name = "Christian Helle",
        Address = "Somewhere",
        City = "Over",
        PostalCode = "The",
        Country = "Rainbow",
        Email = "christian.helle@yahoo.com",
        Phone = "1234567"
    };

    // Create record
    dataContext.Contact.InsertOnSubmit(contact);
    dataContext.SubmitChanges();

    // Retrieve records
    var contacts = dataContext.Contact.ToList();
}