How to make relative database path in app.config using SQLite / entity structure

I want to be able to use the relative path to use SQLite DB on more than 1 pc. The connection string in app.config is as follows:

<add name="DBPersonEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite;provider connection string='data source=&quot;C:\Users\Dreeze\Documents\Test DB2\DBPerson.s3db&quot;'" providerName="System.Data.EntityClient" /> 

The DB file is in the same folder as the application ... I would like to make the path relative, so it refers to the application folder. Can someone help me change this connection string?

+4
source share
2 answers

Use this connectionString

 <add name="DWContext" connectionString="Data Source=|DataDirectory|DBPerson.s3db" providerName="System.Data.SQLite" /> 

Then set the DataDirectory path to your code before initializing the Context objext.

 string executable = System.Reflection.Assembly.GetExecutingAssembly().Location; string path = (System.IO.Path.GetDirectoryName(executable)); AppDomain.CurrentDomain.SetData("DataDirectory", path); 
+1
source

You should be able to use the data directory lookup string:

 provider connection string='data source=&quot;|DataDirectory|DBPerson.s3db&quot;' 
0
source

Source: https://habr.com/ru/post/1434346/


All Articles