0

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?

3
  • is it a winforms project? Commented Feb 25, 2014 at 10:24
  • "app.config" is in a console project, database context in an external library Commented Feb 25, 2014 at 10:25
  • Have you tried Data source=|DataDirectory|DefaulstStore.sdf; ? Commented Feb 25, 2014 at 10:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.