问题
In development, i.e., when using Visual Studio and its built-in IIS Express instance, I have set up my web app to use LocalDB, with the data MDF file located in a directory on my computer but not inside the project directory, e.g., "C:\MyAppData\MyAppData.mdf", not "C:[...]\MyAppProject\App_Data\MyAppData.mdf."
This is working just fine when using Visual Studio/IIS Express, and the connection string being used looks something like this:
<add name="MyAppEntities" connectionString="metadata=res://*/MyAppModel.MyAppModel.csdl|res://*/MyAppModel.MyAppModel.ssdl|res://*/MyAppModel.MyAppModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0;attachdbfilename=C:\MyAppData\MyAppData.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
My question is, after I deploy the app to a development web server, is it possible that the app can connect to that same MDF file hosted on my computer? If so, how? If not, why not? Note that I am not publishing the MDF file to the development web server: I am trying to access the MDF file on my computer from the app that is running on a web server.
My assumption would be that the web app would use the connection string to look for that MDF file on whoever's computer is running the app, but apparently it is not this simple and may not even be possible?
回答1:
It won't work. You need to setup, perhaps, a SQL Server Express instance in a local PC, and inside create a database similar to your MDF (or just export it outright). You can connect to this SQL Express instance changing the connectionsString to a (example) "SERVERNAME" and referencing the database. A great example is at http://www.connectionstrings.com/. SQL Server Express is free, and also fairly more robust than anything internal in VS.
来源:https://stackoverflow.com/questions/29355332/can-i-connect-to-localdb-mdf-file-on-computer-from-a-web-app-that-is-being-run-o