Deployed SQLite database is empty after deployment of Universal App

穿精又带淫゛_ 提交于 2020-01-24 14:16:20

问题


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

SQLite error 1 no such table Companies

It corresponds to the code

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

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

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.


回答1:


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

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!



来源:https://stackoverflow.com/questions/54211841/deployed-sqlite-database-is-empty-after-deployment-of-universal-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!