简体   繁体   中英

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!

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

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.

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