This week we pulled a database from a client site because they reported errors in a WPF application we created. When I copied the .mdf database into my environment and tried to run the C# code against it in visual studio. The following message was produced.
"The database 'myDatabase' cannot be opened because it is version 904. This server supports version 869 and earlier. A downgrade path is not supported."
This project is using the LocalDb as the datasource and apparently the client was using a newer version than what I am using in my development site.
You should always be cautious when upgrading a LocalDb database as they cannot be downgraded. And as usual always make backups of all databases before proceeding.
Warning! Doing the following steps will upgrade all existing localdb database and you will need to reattach them to the localdb. This can be done using SQL Server Management Studio or with command line.
Below are the steps I used to upgrade the LocalDb instance in my development environment.
Step 1: Install Newest version of MS SQL Server Express
Download and install the newest version of MS SQL Server Express
Choose the custom install option.
Warning: As you move through the installation wizard at the step labeled "Features" you need to select the checkbox next to "localdb" BUT there is a bug in this installation package. If you click anywhere in this windo it will move you to the next step. So be careful to make sure you click directly on the checkbox.
After you have successfully upgraded SQL Server Express restart the computer.
Step 1: Stop, Delete & Recreate LocalDb
When the computer restarts you will need to stop and delete the current version of localdb. And then create a new instance. This is the step will cause any existing database instance to detach from localdb.
Use command line for this step (cmd.exe).
To Stop LocalDb: "sqllocaldb stop MSSQLLocalDB"
To Delete LocalDb: "sqllocaldb delete MSSQLLocalDB"
To Create LocalDb: "sqllocaldb create MSSQLLocalDB"
The return message when a new instance is created should tell you the version of the instance created. The version for 2019 is 15.x.x.x. If the correct version isn't created MS SQL Server Express was not upgraded to the correct version in Step 1. You will need to start back at that step.
To Start the new LocalDb: "sqllocaldb start MSSQLLocalDB"
The localdb has been upgraded. You will need to reattach any database instances.
It is also important to note all log files will need to be deleted. If they aren't you may encounter additional errors when running connection strings.
The easiest way to re-attach database is using SQL Server Management Studio (SSMS) which can be downloaded for free.
For my purpose I use C# in my project to reattach the database when the program is ran.
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\myDatabase.mdf;Integrated Security=True"
Hope this helps someone.