Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to be able to use a relative path to use a SQLite DB on more than 1 pc. the connectionstring in the app.config looks like this now:

<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 app... i would like to make the path relative so it refers to the apps folder. Can anyone help me change this connectionstring?

like image 338
Dreeze Avatar asked Nov 04 '22 16:11

Dreeze


1 Answers

Use this connectionString

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

Then set DataDirectory path on your code before initializing Context objext.

string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
string path = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", path);
like image 194
hkutluay Avatar answered Nov 08 '22 04:11

hkutluay