1
votes

I have a Windows Forms application that uses LocalDb and DbContext. This works fine on my development box but I have not been able to get it to work when deployed to another box.

In my app.config, I instantiate a named LocalDb instance in with:

  <system.data.localdb>
    <localdbinstances>
      <add name="LocalProvergience" version="11.0" />
    </localdbinstances>
  </system.data.localdb>

My connection string in is:

<add name="LocalProvergienceEntities"
         connectionString="metadata=res://*/ProvergienceModel.csdl|
                                    res://*/ProvergienceModel.ssdl|
                                    res://*/ProvergienceModel.msl;
                                    provider=System.Data.SqlClient;
                                    provider connection string=&quot;data source=(localdb)\LocalProvergience;
                                    initial catalog=LocalProvergience;integrated security=True;
                                    multipleactiveresultsets=True;
                                    App=EntityFramework&quot;"
         providerName="System.Data.EntityClient" />

If I include:

   AttachDbFilename=|DataDirectory|\LocalProvergience.mdf;

in the connection string, I get an "Invald key value for attachdbfilename" error.

If I exclude AttachDbFilename, I get "A network-related or instance-specific error occurred while establishing a connection to a SQL server" error.

Several questions:

  1. The providerName is set to "System.Data.EntityClient" should this be "System.Data.LocalDb"?

    1. Would anyone be able to post or direct me to an example of a correct app.config for LocalDb and DbContext?
1
As an experiment - I created an instance of a server named LocalProvergience on the target machine and started the instance via the sqllocaldb command line. SqlLocalDb i reports LocalProvergience v11.0 When I run the application, I get the exact same error.Doug Kimzey
Is there a way to programmatically obtain the connection string being used by DbContext?Doug Kimzey
I have tried connection strings for shared instances as well as a connection string using the named pipe obtained by SQLLocalDb info MyInstance commands. I am using .Net 4.5 on the target machine. All connections to LocalDb fail with "A network-related or instance-specific error occurred while establishing a connection to the SQL Server." The only change is with a named pipe connection, the provider says (provider: Named Pipes Provider, error 40...Doug Kimzey

1 Answers

1
votes

When debugging what does AppDomain.CurrentDomain.GetData("DataDirectory") equal when running the app? If null you can set it with

AppDomain.CurrentDomain.SetData(
  "DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));

I just ran into this, using but my AttachDBFilename = |DataDirectory|database.mdf, I think it doesn't need the extra \ because of the Path.Combine.

Also I notice the v11.0 property Auto-Create when running SQLlocalDB info, is Yes, not sure if that makes a difference when specifying named instances.