Acrotygma
Acrotygma

Reputation: 2569

Entity Framework connection string: SQLCe file location

I've specified the following entry in my app.config

<add name="DefaultStoreConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=DefaultStore.sdf" />

Whenever I start my program and access the database, the database gets created at the following location

MyAppDir/DefaultStore.sdf

However, when I work with the Package Manager Console in order to create migrations, it creates the database there:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\DefaultStore.sdf

The file name is right, so I guess Entity Framework just takes a relative path from the calling application, in case of the Package Manager Console: Visual Studio. Can I specificy my application directory inside app.config?

Upvotes: 0

Views: 186

Answers (1)

Manish Mishra
Manish Mishra

Reputation: 12375

try this:

set your connection like this:

<add name="DefaultStoreConnection" providerName="System.Data.SqlServerCe.4.0" 
     connectionString="Data Source=|DataDirectory|\DefaultStore.sdf" />

now you can define the meaning of |DataDirectory| like this inside your main function (of program.cs) make sure it executes before any kind of database interaction :

string fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
AppDomain.CurrentDomain.SetData("DataDirectory", fileName);

here in the Environment.SpecialFolder enum, you get to choose the executing directory as well, you can provide your custom directory aswell.

Upvotes: 2

Related Questions