Why is the LINQ to SQL database not stored in the WP7 emulator?

I am trying to save a very simple username / password in WP7 7.1 beta 2 using LINQ to SQL. So far, everything is working as expected (using the repository / model classes below)

I can create a new database, insert a record and query for this row (and it does not return any problems to the regular user). But the next time I spread the emulator, the db context returns false when I execute this expression "db.DatabaseExists ()"

Is this normal behavior for the emulator, or am I not telling LINQ to keep it between sessions?

Thank you in advance

repository class

public class UserRepository : IDisposable { private readonly UserDataContext context; public UserDataContext Context { get { return context; } } public UserRepository() { context = new UserDataContext(UserDataContext.DBConnectionString); CreateDatabase(); } private void CreateDatabase() { if (context.DatabaseExists() == false) { context.CreateDatabase(); } } public User GetByID(int id) { return context.GetTable<User>().FirstOrDefault(e => e.UserId.Equals(id)); } public User GetByUsername(string username) { return context.GetTable<User>().FirstOrDefault(e => e.UserName.Equals(username)); } public void Save(User user) { if (user.UserId > 0) { context.GetTable<User>().Attach(user, true); } else { context.GetTable<User>().InsertOnSubmit(user); } context.SubmitChanges(); } public List<User> GetAll() { return context.GetTable<User>().ToList(); } public int Count() { return context.GetTable<User>().Count(); } public void Dispose() { if (context != null) { context.Dispose(); } } } 

model

 [Table] public class User { [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] public int UserId { get; set; } [Column] public string UserName { get; set; } [Column] public string Password { get; set; } } 

context

 public class UserDataContext : DataContext { public static string DBConnectionString = "Data Source=isostore:/User.sdf"; public UserDataContext(string connectionString) : base(connectionString) { } public Table<User> Users; } 
+4
source share
2 answers

This is the expected behavior.

The database (and other data) is stored in isolated storage on the emulator. The emulator cleans up the isolated storage upon shutdown ( see the first note on MSDN here ), so your database has been deleted.

+2
source

Isolated Storage Explorer allows you to create and restore snapshots of isolated emulator storage. You might want to examine this to save the state of your database until the emulator closes, and then restore it after the emulator restarts.

http://msdn.microsoft.com/en-us/library/hh286408(v=vs.92).aspx

+3
source

All Articles