问题
Two-Part Question.
I have created an ASP.NET MVC 5 application using VS 2015 Update 3. I am quite a ways along with the project on my local machine. Suddenly, I can no longer connect via SQL Server Object Explorer to the .mdf
database file and get the following error:
The database 'aspnet-DevilsTowerMVC-20180327093314' cannot be opened because it is version 852. This server supports version 782 and earlier. A downgrade path is not supported. Could not open new database 'aspnet-DevilsTowerMVC-20180327093314'. CREATE DATABASE is aborted. Cannot attach the file 'D:\VS2015\DevilsTowerMVC\App_Data\aspnet-DevilsTowerMVC-20180327093314.mdf' as database 'aspnet-DevilsTowerMVC-20180327093314'.
I have SQL Server 2012 (11.0.5388.0) installed on my machine. The project was created using VS 2015. I saw a similar question on SO but this did not provide a definitive answer.
I just created another ASP.NET MVC 5 app in VS 2015 and the login worked perfectly (says SQL Server 12.00.2000). So I don't understand how my existing project mysteriously reverted to an older version?
From my research, SQL Server 2016 has an internal database version number of 852, and SQL Server 2014 and has an internal DB version number of 782. My installation of SQL Server 2012 has an internal DB version number of 706, so I don't know why it's mentioning '782' in the error message.
(Q1) If I cannot downgrade then my only option may be to install SQL Server 2016 Express? Is this correct and what are the steps to do this correctly? If not, is there a way to 'reset' it - newly created project works just fine?
(Q2) How do I convert this to use a regular non-file-based SQL Server database in SSMS?
My web.config:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DevilsTowerMVC-20180327093314.mdf;Initial Catalog=aspnet-DevilsTowerMVC-20180327093314;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
UPDATE 1
I downloaded the SQL Server 2016 SP1 Express Edition from here. I selected the Download Media/LocalDB option. It appeared to install correctly. However, when I select the DefaultConnection in Server Explorter and hit refresh, I get the exact same error as before. I tried all combinations in my connection string, such as: (LocalDb)
, (LocalDb)\v11.0
and (LocalDb)\MSSQLLocalDB
.
When I look in the C:\Windows\SysWOW64 folder, I see the SQLServerManager11.msc configuration manager still installed.
UPDATE 2
I deleted the .mdf file from the App_Data folder and tried recreating it by registering a new user. A DefaultConnection.mdf file was created, but when I tried adding it in Server Explorer I got the same version error message as before. Can anyone suggest a way to redeem the project login system?
回答1:
1) Sadly you can't downgrade, you'll have to get the same version. 2) You can restore database with .mdf files as far as I know, but technically they should all be file based. Still if you have the mdf file you can restore it and then change your connection string accordingly.
回答2:
I managed to solve this problem with the aid of an answer by Yair M to this question.
I first physically uninstalled all versions of LocalDb aside from the v11.0 one which corresponded to my version of SQL Server 2012, using the Control Panel --> Programs & Features.
Then using the command line, I issued the following commands:
sqllocaldb delete "v11.0"
sqllocaldb create "v11.0"
This solved the issue where the file based instance of LocalDb was being wiped and not re-created when I tried to register a new user.
At this point I could register a new user without any error messages. However, I wanted to move away from LocalDB.
So, I created an empty database in SSMS and attached the .mdf file to it. I updated my connection string to use an actual DB and Integrated Security (for local development). I got some permissions errors when running the app, but was able to remedy this by creating a new user and giving it read and write access.
<add name="DefaultConnection"
connectionString="Initial Catalog=DevilsTower;Data Source=Development;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
来源:https://stackoverflow.com/questions/51164180/sql-server-express-connection-to-mdf-file