NovaDev
NovaDev

Reputation: 2961

What is the correct connection string variable in Azure for Sqlite?

Trying to use a Sqlite database on Azure with the default dotnet core example app with authentication and authorization. On my local machine, it puts the db at /Users/me/Projects/myProj/bin/Debug/netcoreapp1.0/myProj.db - and my appsettings.json contains this:

"ConnectionStrings": {
    "DefaultConnection": "Data Source=myProj.db"

So now when I upload to azure without appsettings.json, I have to configure the connection in the Application Settings -> Connection strings section of azure. Part of the issue I'm having is that when I git push from my localhost to azure, the location of the db changes. It gets moved to d:\home\site\wwwroot. I know that I have to change the connection string, but my various changes to what I think should make it point there have failed - do I include the "Data Source" part? Or ... do I need the quotes? I saw a related post that in a comment to the question that gave an interesting answer, but that didn't work.

What is the right way to enter this into azure's connection strings piece? Azure connection strings entry

Upvotes: 0

Views: 1855

Answers (1)

yoape
yoape

Reputation: 3335

The connectionstring to SQLite should contain DataSource like this:

Data Source=c:\path\mydb.db;

Your SQLite database should be located in the App_data folder of your project. You need to create it manually for .NET core projects.

You could then use Kudu to inspect your deployed website and figure out where the database file is placed after deployment if you are unsure. Log in to Kudu by navigating to https://yourwebsite.scm.azurewebsites.net, i.e. your websites Uri but put scm between the name and azurewebsites.net. Log in using the same credentials you use for Azure portal.

You should now see the Kudu portal. Go to Debug console and select CMD or PowerShell (which ever you like). In the top part you will now see a file explorer and you should be able to find your database file here, if it has been properly deployed.

enter image description here

Just navigate to D:\home\site\wwwroot and see if you have an App_data folder. Once you found your database you can put that path into you connection string in the Azure portal and Restart the website.

Another way would be to build the connection string in your code and allowing ASP.NET to mapp the folder for you:

ConnectionString = "Data Source=" +
    HttpContext.Current.Server.MapPath(@"\App_Data\mydb.db")‌​;

or:

ConnectionString = "Data Source=" +
    AppDomain.CurrentDomain.GetData("DataDirectory") + "mydb.db";

Beware though, the space available to your website in App_data (or any other disk location) for a website is limited (depending on your tier) and if your database eats up all that your site will crash, your database wont save data and everyone will be sad.

Another option for you would be to use blob storage and put your SQLite database file there.

Upvotes: 5

Related Questions