0

I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF). I am trying to simulate creation of database per project. Meaning that user could open file dialog window, select / create desired folder for project and then access existent or create new database. Which leads me to a question how can i create / read database connection file per specific project.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="NerdDinners"
         connectionString="Data Source=\Projects\NerdDinners.sdf"
         providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>
</configuration>
2
  • It is offtopic a bit, but i would recommend to use NHibernate instead of EF. I've tried to use EF at first, but then I found that NHibernate is much easy to configure for Code-First approach. If you want, I can provide you a link for a fast start Guide. Commented Nov 24, 2011 at 12:25
  • Actually EF with poco is quite easy for code first approach, but this seems to be more a matter of personal taste. I was going to use NHibernate with SQLite at the beginning but dropped the idea due to limited time to research. I am open for any other alternative approach though. Thanks Commented Nov 24, 2011 at 12:35

1 Answer 1

1

Your connection string is stored in your app.config. When executing your WPF program you can only access the current app.config of the executing assembly. So there is no default way to open different app.config files for each project.

You could however use OpenExeConfiguration which lets you specify a path to a config file.

Or you could save multiple configration strings in the app.config of the executing application and give them different names. By using SqlConnectionStringBuilder you can manage the content of the string.

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

5 Comments

Actually I am not going to load default project when the application starts. The process is the following: Start app-> Create project (new folder, new connection file,new database and ) -> Load project (read connection file and load database)
Then you could use the SqlConnectionStringBuilder to build a connection string that points to your project/database and pass that as to the constructor of your DbContext so it will use this connection string.
Thanks, for pointing to the right direction... Do you have by any chance any sample to reproduce the above connectionstring?
Hi, I've managed to do it using stackoverflow.com/questions/7458513/… and post about it webpixel.gr/blog/net-blog/…

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.