Black Dynamite
Black Dynamite

Reputation: 4147

'Local Database Runtime: Cannot create named instance

I have a C# app that I am trying to make use of using LocalDB. As of now, when I start up my app, I am greeted by this error.

Cannot create named instance.

The parameter for the LocalDB Instance API method is incorrect. Consult the 
API documentation.

My App.config looks like this.

<system.data.localdb>  
   <!--    I saw this on another website, I'm not sure if it's neeed or not.-->
  <localdbinstances>  
    <add name="MSSQLLocalDB" version="mssqllocaldb" />  
  </localdbinstances>  
</system.data.localdb>
<connectionStrings>
<add name="DiaProdConnection" 
     connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;Integrated Security=True;MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient" />
 </connectionStrings>
<entityFramework>
   <defaultConnectionFactory 
       type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

When I run Sqllocaldb.exe versions, I get the following output.

..> SqlLocalDB.exe versions
  Microsoft SQL Server 2012 (11.0.3000.0)
  Microsoft SQL Server 2014 (12.0.4459.0)
  Microsoft SQL Server 2016 (13.0.1601.5)

Ideally, the application would start up, and when it saw the db didn't exist, it would create it.

The following line does work in development, but if fails on my test system which has the 2014 version of SQLLocalDB which is why I've decided to use the new string. Also, folks say the old string is deprecated and only works for 2012.

  <add name="DiaProdConnection" connectionString="data source=(localdb)\v11.0;initial catalog=DiaDb;integrated  security=True;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=15;Connection Lifetime=0;Min Pool Size=10;Max Pool Size=20;Pooling=true;" providerName="System.Data.SqlClient" />

Any help is greatly appreciated.

Upvotes: 1

Views: 1173

Answers (1)

Black Dynamite
Black Dynamite

Reputation: 4147

  1. Purge by fire your computer of all non-SQL 2014 LocalDb or higher instances. As of this writing, I'm using the SQL-2014 version.

  2. Make your EF connection xml in the App.Config look something like this.

     <system.data.localdb>
       <localdbinstances>
         <add name="MyNamedInstance" version="12.0" />
       </localdbinstances>
      </system.data.localdb>
     <connectionStrings>
        <add name="DiaProdConnection" 
           connectionString="Data Source=(localdb)\MyNamedInstance;Initial 
           Catalog=MovieDB;Integrated 
           Security=True;AttachDbFilename=|DataDirectory|MovieDB.mdf" 
           providerName="System.Data.SqlClient" />
     </connectionStrings>
    
  3. Many examples will have that field |DataDirectory| in their connection string. Unfortunately, what they won't tell you is that that string isn't a special string known by the framework, but is instead a variable that you have to assign and set yourself. For that, I call this code at the entry point of my application.

    AppDomain.CurrentDomain.SetData("DataDirectory",
        Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)); 
    

Keep in mind, that if the DB exists on the system but in a different location, the connector will throw an error.

Running

Sqllocaldb.exe i

Should help you see if an instance exists and from there you can decide if it needs to be deleted.

I'm horrified by how poor the documentation is on the errors and how obnoxious the whole thing seemed.

Upvotes: 2

Related Questions