Search code examples
sql-serverentity-framework-4connection-stringlocaldb

The underlying provider failed on ConnectionString (|DataDirectory| and LocalDB)


I'm getting the exception

The underlying provider failed on ConnectionString.

with inner exception

Invalid value for key 'attachdbfilename'

on a windows 7 home premium virtual machine as i'm trying to deploy and debug my application.

However, on my development machine, the application just launches without problems.

the connection string is :

<add name="AssetsLocalDBEntities" connectionString="metadata=res://*/AssetsLocalDB.csdl|res://*/AssetsLocalDB.ssdl|res://*/AssetsLocalDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(localdb)\v11.0;attachdbfilename=|DataDirectory|\AssetDatabase.mdf;initial catalog=AssetDBSource;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Steve Wellens has mentioned that the connection string contains garbage in this topic: The underlying provider failed on ConnectionString

yet, as Maxim Gueivandov has replied, this should valid connection string, as it's directly generated by the EntityFramework's edmx designer. At least, i believe it to be valid, as it does work on the development machine.

Also in another topic (Entity Framework - The underlying provider failed on ConnectionString), the accepted answer is to remove the &quot; and replace them by \", as the string is a literal in the c# code, yet i still have my connection string in xml, so &quot; cannot simply be replaced. on-connectionstring

The .NET version installed on the virtual machine is 4.0.30319 and MsSQL Server Express LocalDB is 11.0.2318.0, so i believe they are both up to date.

Edit: If i remove "|DataDirectory|" from the connection string i get the following error on my development machine:

Cannot attach the file 'AssetDatabase.mdf' as database 'AssetDBSource'.

So i also remove "initial catalog=AssetDBSource" and i get

An attempt to attach an auto-named database for file AssetDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

i can emagine this is because it doesn't know where to look? so, i provide it the full path (C:..\AssetDatabase.mdf) and it works.

So the remainder of the question is: how do i get relative paths working with LocalDB?


Solution

  • I believe the target machine doesn't have the Update 4.0.2 or later for .NET 4 installed. The .NET 4 version number can be confusing, as version 4.0.30319 seems to be used by all .NET 4 builds. In effect it does not mean your machine has the Update 4.0.2.

    Can you install .NET 4 Update 4.0.3 (latest .NET 4 update as of today) on the target machine and see if the problem goes away?

    Or just verify that the update is installed - just build a simple application in .NET 4 that uses SqlClient to connect to LocalDB using the connection string, and record the error information in case you still hit any errors. The EF error message is fairly generic, and I don't know how to get a detailed error from the underlying SqlClient provider.

    Alternatively you could also migrate it to .NET 4.5.