0

I want to create a local DB in the folder of my application using EF6 Code First approach in a WPF application. I have tried in different ways to configure the connection string in App.config in order to achieve my goal but none was successful. Can someone help me?

1 Answer 1

1

If I understand correctly, you are successfully creating a LocalDB database. But you want the database located in the folder of your application executable instead of the default location. Is that correct?

You've probably got a connection string setting that looks something like this:

<add name="MyDatabase" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyDatabase.mdf" providerName="System.Data.SqlClient" />

The important part is the AttachDBFilename property. Make sure it starts with "|DataDirectory|".

Now, somewhere in your app (I do it in OnStartup), you'll want to change the value of the DataDirectory domain property. To change this to the folder your executing assembly is sitting in, try this:

AppDomain.CurrentDomain.SetData("DataDirectory", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));

That should do the trick.

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.