steve
steve

Reputation: 917

EF Migrations Database connection string is wrong

When I use update-database -verbose and check the connection string it is wrong, no wonder the command returns with a crazy list of errors. In app.config my conn string is:

dd name="TESTDBConnString" connectionString="data source= .; initial catalog=TESTDB; 
      integrated security=True" providerName="System.Data.SqlClient"

And in my context class my constructor is:

public class MyContext : DbContext, IContext
{

    public MyContext() : base("TESTDBConnString")
    {

    }

    public DbSet<TestModel> TestModel { get; set; }
}

Yet when I run the verbose command I see .\SQLEXPRESS being used as the database source and I cannot even use that in SSMS so can someone show me how to get it to simply take '.'. Thanks!

Upvotes: 0

Views: 998

Answers (2)

Nick Sinitsin
Nick Sinitsin

Reputation: 247

I can't comment that's why i will make a suggestion. In .Net core app you need to have a specific structure in appsettings.json Like

{
"ConnectionStrings": {
  "TESTDBConnString": "server=localhost\\SQLEXPRESS;Initial Catalog=my-DB;Integrated Security=true;Trusted_Connection=True;connect timeout=100;"
  }
}

For login-password connection string a little bit different.

In this case in the context for empty constructor you can use like. And all should work when you call Update-Database.

Upvotes: 1

thejonarnold
thejonarnold

Reputation: 371

it looks like your connection string format is invalid.

from the MSDN:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <connectionStrings>
    <add name="BloggingDatabase"
         connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" />
  </connectionStrings>
</configuration>

it seems like you need to specify Server and Database for current Entity Framework apps connecting to SQL SERVER

Upvotes: 1

Related Questions