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>
Jim
  • 2,760
  • 8
  • 42
  • 66
  • 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. – Seekeer Nov 24 '11 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 – Jim Nov 24 '11 at 12:35

1 Answers1

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.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • 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) – Jim Nov 23 '11 at 22:27
  • 1
    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. – Wouter de Kort Nov 23 '11 at 22:29
  • Thanks, for pointing to the right direction... Do you have by any chance any sample to reproduce the above connectionstring? – Jim Nov 23 '11 at 22:41
  • Does this help? http://stackoverflow.com/questions/6003085/how-do-i-programmatically-set-the-connection-string-for-entity-framework-code-fi – Wouter de Kort Nov 24 '11 at 07:32
  • Hi, I've managed to do it using http://stackoverflow.com/questions/7458513/is-possible-to-create-a-database-sql-server-compact-on-a-given-path-if-we-use/7458778#7458778 and post about it http://www.webpixel.gr/blog/net-blog/programmatically-set-the-connection-string-for-entity-framework-code-first#more-360 – Jim Nov 24 '11 at 09:57