【问题标题】:Including SQLite DB file with data in the UWP application在 UWP 应用程序中包含带有数据的 SQLite DB 文件
【发布时间】:2018-01-02 13:11:49
【问题描述】:

我正在尝试在 UWP 应用程序中包含一个 SQLite 文件。 SQLite 文件中有许多表,其中填充了数据。

数据库(至少目前)是只读的,我打算使用 EF Core 来访问数据。

所以我有两个问题:

  1. 将此文件与应用程序捆绑在一起的正确方法是什么。
  2. 如何编写文件路径以从 UWP 应用程序访问该文件?

将 .sqlite 文件的“复制到输出目录”设置为“始终复制”似乎会将文件复制到 bin 文件夹,但我似乎无法找出访问它的路径。将 SQLite 连接字符串设置为“Filename=databasename.sqlite”时 - 它似乎在其他地方创建了一个空白数据库文件,因为尝试访问任何表都会引发 table not found 异常。

提前致谢!

【问题讨论】:

    标签: sqlite uwp windows-10 entity-framework-core


    【解决方案1】:

    我的建议是……

    1. 将 .db 文件包含到您的 AppX 包中。要包含它,请将 .db 文件添加到项目的 Assets 文件夹中。
    2. 然后,在第一次启动您的应用程序时,您应该将 .db 文件从 Assets 复制到应用程序的本地文件夹。因为,SQLite 本身无法访问 Assets 文件夹。它只能访问您应用的本地或临时文件夹。

    我已经在 MSDN 论坛上回答了类似的问题。请也参考一下。

    [UWP] SQLite.net Path question?

    【讨论】:

    • 回复一下!此外,正如我发现的那样 - 您需要在 DB 文件上将构建操作设置为“内容” - 否则它不会复制到输出 AppX 文件夹中,即使它位于项目的 Asset 文件夹中。
    【解决方案2】:

    根据@Mamoru Satoh 链接中的答案,什么对我有用

    在 Assets 文件夹中,我将数据库扩展名更改为 .png 否则无法复制,然后在 distination 中数据库名称扩展名是 .db

        public async Task getDataBase()
        {
            StorageFile dbFile = (StorageFile) await ApplicationData.Current.LocalFolder.TryGetItemAsync("TestDb.db");
            try
            {
                if (null == dbFile)
                {
                    // first time ... copy the .db file from assets to local  folder
                    var localFolder = ApplicationData.Current.LocalFolder;
                    var originalDbFileUri = new Uri("ms-appx:///Assets/TestDb.png");
    
                    var originalDbFile = await StorageFile.GetFileFromApplicationUriAsync(originalDbFileUri);
    
                    if (null != originalDbFile)
                    {
                        dbFile = await originalDbFile.CopyAsync(localFolder, "TestDb.db", NameCollisionOption.ReplaceExisting);
                    }
                }
            }
            catch (Exception ex)
            {
            }
    
        }
    

    如果您对从资产文件夹复制的数据库使用实体框架

    public class Emp
    {
        [Key]
        public int EmpID { get; set; }
        public string EmpName { get; set; }
        public int EmpDept { get; set; }
    }
    
    public class DataContext : DbContext
    {
        public DbSet<Emp> Employees { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite("Data Source=TestDb.db");
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Emp>().ToTable("Emp");
        }
    }
    

    您需要在启动应用程序时运行 getDataBase() 函数

    你可以用同样的方式从数据库中查询

      DataContext context = new DataContext();
      string name = (await context.Employees.OrderBy(a => a.EmpName).FirstOrDefaultAsync()).EmpName;
    

    【讨论】:

    • 说,你知道这很奇怪,因为我看到有人说他们将 .pdf 文件和 .xml 文件存储在 Asset 文件夹中,但如果不重命名它,我实际上无法让 db 文件工作.png,就像你说的。此外,我已经与网站合作多年。我没有注意到仅将文件复制到 Asset 文件夹不起作用,就像在 Asp.Net 网站中那样。在这上面浪费了相当多的时间。哈哈。
    • 我同意你的观点,这真的很奇怪!
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2020-03-26
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多