问题
I have a VS 2012 solution setup like this:
- EF Model Project
- EF Model Test Project
- ASP.NET MVC 4 Application
- WCF Data Services Project
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?
回答1:
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:
- In a Test project it uses local db catalogue with the FQDN of your DbContext with an MDF file located at \\.mdf.
- In an ASP.NET MVC project it attempts to load the mdf from \app_data\.mdf
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/15960535/how-to-share-a-localdb-instance-across-all-projects-in-a-solution