这是db初始化代码:
using (MyDbContext db = new MyDbContext("Data Source=isostore:/my_db.sdf"))
{
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
}
代码在调试版本上运行良好,但在发布到Windows手机商店后失败。
堆栈跟踪:
The database file cannot be found. check the path to the database. [ Data Source = C:\Data\Users\DefApps\AppData\{A1F26781-AA93-432A-AD80-B02825C3BBA8}\Local\my_db.sdf ]
at System.Data.SqlServerCe.SqlCeConnection.Open(Boolean silent)
at System.Data.SqlServerCe.SqlCeConnection.Open()
at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult, Boolean isCompiledQuery)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries, Boolean isCompiledQuery)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
at MyApp.Models.SomeModel.restoreObjectData()
感谢您的帮助。
答案 0 :(得分:1)
您需要将数据库复制到isostore文件夹,如果使用SQL CE Toolbox DataConntext,则可以使用以下代码:
if (!db.DatabaseExists())
{
db.CreateIfNotExists();
}
答案 1 :(得分:0)
对不起伙计们,我发现了一个问题。我在这里写了数据库初始化代码。
public App()
{
// Show graphics profiling information while debugging.
if (System.Diagnostics.Debugger.IsAttached)
{
db.create();
}