部署通用应用程序后部署的 SQLite 数据库为空

Deployed SQLite database is empty after deployment of Universal App

我在 UWP 中使用 SQLite 数据库。它有几个表,位于 Assets 文件夹中,构建操作标记为 "Content".

在开发机下运行良好。 但是在部署到 MS Windows 10 平板电脑后出现错误

SQLite error 1 no such table Companies

对应代码

using (MobileContext db = new MobileContext())
{
   companiesList.ItemsSource = db.Companies.ToList();
   ...
}

var createdResult = Database.EnsureCreated(); // It gives false so

我假设基于 https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.databasefacade.ensurecreated?view=efcore-2.1 数据库存在。

我试过了

optionsBuilder.UseSqlite("Data Source=" + ApplicationData.Current.LocalFolder.Path + @"\Mobile.db");

optionsBuilder.UseSqlite("Data Source=Mobile.db");

有线索吗?谢谢!

P.S。我使用 Microsoft.EntityFrameworkCore 访问 SQLite。

P.S。 #2 我试过这个解决方案 https://social.msdn.microsoft.com/Forums/en-US/1a933b13-09ee-46ec-9045-e2f567b6048c/uwp-sqlite-error-1-no-such-table-name-table?forum=wpdevelop 但它不起作用。

源自 official document

Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. They typically do not contain sensitive information, and do not need to be changed as an application is deployed. As such, these connection strings are usually fine to be left in code, as shown below. If you wish to move them out of code then UWP supports the concept of settings

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
            optionsBuilder.UseSqlite("Data Source=blogging.db");
    }
}

而db文件的默认路径是applicationLocalFolder。看起来像这样 C:\Users\vxx\AppData\Local\Packages\e045f456-27d9-4966-b639-01e2281b249f_7jxxxxxxxxxx\LocalState。如果你的配置和上面一样,在新机器上部署时,db文件的内容是空的。

OP更新

我刚刚评论了 class 的一些装饰,例如 [Table("Companies")][Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)],现在可以使用了!