本文介绍了部署通用应用程序后部署的SQLite数据库为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在开发机器下它工作正常.但是在部署到 MS Windows 10 平板电脑后出现错误

SQLite 错误 1 ​​没有这样的表公司

对应代码

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

var createdResult = Database.EnsureCreated();//它给出 false 所以

我假设基于 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");

有什么线索吗?谢谢!

附言我使用 Microsoft.EntityFrameworkCore 来访问 SQLite.

附言#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 但它不起作用.

解决方案

源自 官方文档,

UWP 应用程序中的连接字符串通常是仅指定本地文件名的 SQLite 连接.它们通常不包含敏感信息,并且不需要在部署应用程序时进行更改.因此,这些连接字符串通常可以留在代码中,如下所示.如果您希望将它们移出代码,那么 UWP 支持设置的概念

公共类 BloggingContext : DbContext{公共数据库集博客 { 得到;设置;}公共 DbSet帖子{得到;设置;}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseSqlite("Data Source=blogging.db");}}

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

OP 更新

我刚刚评论了类的一些装饰,例如 [Table("Companies")][Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 并且它现在可以工作了!

I use in SQLite database in UWP. It has a few tables and is located in Assets folder and has build action marked as "Content".

Under development machine it works fine.But after deployment to MS Windows 10 tablet it has the error

It corresponds to the code

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

I assume based on https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.databasefacade.ensurecreated?view=efcore-2.1 that database exists.

I have tried

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

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

Any clue folk? Thanks!

P.S. I use Microsoft.EntityFrameworkCore to accees SQLite.

P.S. #2 I have tried this solution https://social.msdn.microsoft.com/Forums/en-US/1a933b13-09ee-46ec-9045-e2f567b6048c/uwp-sqlite-error-1-no-such-table-name-table?forum=wpdevelop but it does not work.

解决方案

Derive from official document,

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

And the default path of db file is application LocalFolder. It looks like this C:\Users\vxx\AppData\Local\Packages\e045f456-27d9-4966-b639-01e2281b249f_7jxxxxxxxxxx\LocalState. If your configuration is same as above, when deploy in new machine, the content of db file is empty.

OP Update

I just commented some decorations of the class like [Table("Companies")] and [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] and it works now!

这篇关于部署通用应用程序后部署的SQLite数据库为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:18