为什么 LINQ to SQL 数据库未保留在 WP7 模拟器中?

发布于 11-29 15:16 字数 2351 浏览 0 评论 0原文

我正在尝试使用 LINQ to SQL 在 WP7 7.1 beta 2 中保留一个非常简单的用户名/密码组合。到目前为止,一切都按预期工作(使用下面的存储库/模型类),

我可以创建一个新数据库,插入一条记录并查询该行(并且它返回持久用户没有问题)。但是下次我启动模拟器时,当我执行此表达式“db.DatabaseExists()”时,数据库上下文返回 false,

这是模拟器的正常行为还是我没有告诉 LINQ 在会话之间保留此行为?

预先感谢您

存储库类

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();
        }
    }
}

模型

[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; }
}

上下文

public class UserDataContext : DataContext
{
    public static string DBConnectionString = "Data Source=isostore:/User.sdf";

    public UserDataContext(string connectionString) : base(connectionString) { }

    public Table<User> Users;
}

I'm trying to persist a very simple username/password combo in WP7 7.1 beta 2 using LINQ to SQL. So far everything works as expected (using the repository/model classes below)

I can create a new database, insert a record and query for that row (and it returns the persisted user no problem). But the next time I spin up the emulator the db context returns false when I exec this expression "db.DatabaseExists()"

Is this normal behavior for the emulator or am I not telling LINQ to persist this 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇2024-12-06 15:16:51

隔离存储资源管理器允许您创建和恢复模拟器隔离存储的快照。您可能想尝试在关闭模拟器之前使用它来保存数据库的状态,然后在重新启动模拟器后恢复它。

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

The Isolated Storage Explorer lets you create and restore snapshots of the Emulator's isolated storage. You might want to explore using this to save the state of your database prior to closing down the emulator, and then restoring it once you restart the emulator.

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

荒芜了季节2024-12-06 15:16:51

这是预期的行为。

数据库(和其他数据)存储在模拟器上的独立存储中。模拟器在关闭时清除独立存储(请参阅第一个请注意 MSDN 此处),因此您的数据库将被删除。

This is expected behavior.

The database (and other data) is stored in Isolated Storage on the emulator. The emulator clears Isolated Storage on shutdown (see the first Note in MSDN here), so your database is deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文