question

BitSmithy-4663 avatar image
0 Votes"
BitSmithy-4663 asked ·

[UWP] [EF] How to deal with database in specified folder

Hello,


In my UWP app I create a folder:


StorageFolder appFolder = ApplicationData.Current.LocalFolder;
databaseFolder = await appFolder.CreateFolderAsync(databaseFolderName, CreationCollisionOption.OpenIfExists);


now I use EF to deal with my SQLite database


 public class myDbContext : DbContext 
 {
     public DbSet<MyClass> MyClasses{ get; set; }
 }



  1. How to create a new database in &#34;databaseFolder&#34;

  2. How to connect to database stored in &#34;databaseFolder&#34;

I read a lot of docs about EF, but I couldnt find answer on this question.

uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

yanxiaodi avatar image
0 Votes"
yanxiaodi answered ·

From MS Docs:

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, see the App Settings section of the UWP documentation for details.

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

FYI: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

I think you just need t change the connection string to your file path, eg.

 optionsBuilder.UseSqlite("Data Source=yourDbFolderName/yourDbName.db");



· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.