Search code examples
c#windows-phone-8linq-to-sqlsql-server-ceisolatedstorage

Local Database for Windows Phone 8


I have started working on windows phone development recently. I have come across how to maintain relational database locally from this link : LINQ TO SQL

The program performs operations on the .sdf file.

My question is does the LINQ TO SQL ORM use the SQL Server CE database by default?

If not then which database is it using by default?

And is isolated storage used as a container to contain the database file?


Solution

  • SQL Server CE uses it's own methods to store the database within isolated storage.

    If you want to use SQL Server CE with linq, then you need to define your model classes as a System.Data.Linq.Table object, which will be placed in a class of type System.Data.Linq.DataContext.

    Calling SubmitChanges on the DataContext will push all changes in the context instance to the isolated storage backend automatically as an SQL Server CE database.

    public class MyDataContext : DataContext
    {
        private static MappingSource mappingSource = new AttributeMappingSource();
    
        public Table<Person> People;
        public Table<Item> Items;
    
        // pass the connection string to the base class.
        public MyDataContext() : base("DataSource=isostore:/data.sdf", mappingSource)
        {
        }
    
        ~MyDataContext()
        {
            Dispose(false);
        }
    }
    
    MyDataContext db = new MyDataContext();
    
    // do stuff here
    
    db.SubmitChanges();