Reputation: 4010
I have a VS 2012 solution setup like this:
During development i want to use LocalDB as the backing database to EF. The MVC & WCF projects both use the EF Model to access the data in the database. I would like to share the same LocalDB instance across all projects (MVC, WCF, & Test) but can't seem to get the web.config's setup right.
I'd also like this project setup to be portable across developers machines, IE no absolute paths.
The test project creates & works with a database in c:\users\\.mdf. The MVC & WCF projects expect the file to reside in the AppData folder. I've been manually copying it over as needed, but clearly as the apps change data they get out of sync.
Any suggestions or examples on how to config the projects to share the same instance with relative paths?
Upvotes: 4
Views: 5199
Reputation: 29
I might be wrong, one localDB can only be access one application at a time. If you would like one database can be access by multiple application simultaneously, attach the LocalDB to an express edition SQL server. Create a user in the express using form authentication for each application. Change the ConnectionString to express sql with the username and password. You are good to go.
Upvotes: 0
Reputation: 4010
Putting together the example for minhcat_vo made me figure out a solution.
The problem i was encountering happened when you didn't specify an explicit connection string matching the DbContext derived type's name (or whatever you set as it's annotation). The behavior is different between project types:
I stumbled onto the solution while moving my EF project to MySQL. The MySQL EF provider will not fall back to the DefaultConnection connection string under any circumstances (i guess unless your DbContext explicitly selected it). To get MySQL-EF working i had to put in a connection string matching the context. This keyed me to the answer i was looking for. By using the following connection string for all my projects they all share the same MDF file/LocalDB instance:
<add name="ThingsContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=Things;Integrated Security=SSPI" />
Where ThingsContext is the DbContext instance for my project. You can see my example solution @ https://github.com/drdamour/SharedLocalDB
Upvotes: 1
Reputation: 1361
I would like to share the same LocalDB instance across all projects (MVC, WCF, & Test) but can't seem to get the web.config's setup right
It's probably your connection string problem.
Any suggestions or examples on how to config the projects to share the same instance with relative paths ?
Yes, you can use relative paths in each project's web.config. However, you can handle it by using ConfigurationManager.
Upvotes: 0