Sql Server LocalDB Issue In Asp.NET MVC 4 Project

2019-05-18 11:08发布

问题:

I am getting the following error whenever I am trying to open the db_name.mdf file:

The database '{path}\db_name.MDF' cannot be opened because it is version 706. This server supports version 662 and earlier. A downgrade path is not supported. Could not open new database 'db_name.MDF'. CREATE DATABASE is aborted. An attempt to attach an auto-named database for file {$path}\db_name.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Connection string as follows:

<add name="PasswordDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\db_name.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

I am working on a asp.net mvc 4 project within visual studio.

Any help to get the issue resolved are very much appreciated. Thanks.

回答1:

LocalDB is a new feature introduced in SQL 2012 Express - you will need to install SQL 2012 Express LocalDb if you need to use an MDF in this manner.

Krzysztof Kozielczyk has blogged several entries around on LocalDB here - take note especially of the issues when running under 'Full IIS'.

IMO using LocalDb with ASP.NET is for a developer machine config only - i.e. when you are ready to deploy your (code first?) MVC 4 app to a testing or production environment, you should consider attaching ASPNETDB.MDF more permanently to a SQL Instance.



回答2:

You cannot attach a MVC 4 localDB in SQL Server 2012 Management Studio. You'll need to connect directly.

Open up SQL Server 2012 Management Studio. You can download a free express version here:

http://www.microsoft.com/en-us/download/details.aspx?id=29062

During installation make sure you check the localDB component to be installed.

After installation, open Management Studio and the "Connect to Server" dialog will appear.

Enter (LocalDb)\v11.0 in the Server Name textbox. Use windows authentication and click connect.

Your asp database will show in the manager. You can then script it out to what ever you want and edit the DB and make the appropriate changes to your connection strings in your web config.

Remember that if you're using Code First, which in VS 2013's MVC Internet template uses, you'll need to check your db creation code in your app start folder.

LocalDB was created so you can create data driven web applications without the overhead of a full version of SQL server running on your development machine. When you deploy to a production server, migrate to a full SQL Server instance.

Hope this helps :)



回答3:

Set your database connection string to your Locally installed DB is as below.

Web.config :

<add name="ContextName" connectionString="Data Source=ServerName;Initial Catalog=DBCatalogName;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework"
       providerName="System.Data.SqlClient" />