简体   繁体   中英

Deploying LocalDB with Entity Framework - won't use existing database

I built a code-first Entity Framework application in C# and first deployed it successfully on my development machine.

Now I've redeployed the application to a non-dev machine (running the same OS - W7x64) - I also installed localdb. But it doesn't work.

When the app runs it crashes with the error

Cannot create file 'E:\Temp\WebUtility\WebDB.mdf' because it already exists. Change the file path or the file name, and retry the operation.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

it cannot create the database because the file already exists. But I WANT the app to use the existing database, not create a new one, If I remove the existing database files. the app does start up and proceeds to create new empty database files which is no good.

And if I replace these newly created files, it's unhappy again, so clearly it doesn't believe the database it's been deployed with is valid.

My connection string is as follows:

<connectionStrings>
    <add name="ModelContainer"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;attachdbfilename=E:\Temp\WebUtility\WebDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;" 
         providerName="System.Data.SqlClient" /> 
</connectionStrings>

What am I missing here?

It turns out the version of SQL is slightly different - VS installs SQL Express 2016 SP1, I installed 2014 on the deployment box.

Note - simply uninstalling this and reinstalling the 2016 version doesn't work, you need to manually delete the keys from HKEY_CURRENT_USER\SOFTWARE\Microsoft\Microsoft SQL Server\UserInstances

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM