Reputation: 105
I am super excited to start developing .NET on my Mac but getting frustrated trying to figure it all out. So I have a WebApplication created by calling yo aspnet and out of the box it works great. It comes with a connection to a local db stored in the project. Where I am having trouble is connecting it to a remote DB hosted in my azure portal.
My Connection string below directly from my azure portal. I have updated my appsettings.json "DefaultConnection" property to this value (with the username and password missing below) and I am getting errors like "ArgumentException: Keyword not supported: 'server'." I have tried several different connection strings and none are working.
Admittedly I am new to all of this so I am probably missing something simple but in searching online I have yet to find the solution.
Server=tcp:mydbserver.database.windows.net,1433;Initial Catalog=LH;Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
For reference here is my whole appsettings.json (without username & passwrod...)
{
"ConnectionStrings": {
//"DefaultConnection": "Data Source=LH.db"
"DefaultConnection": "Server=tcp:mydbserver.database.windows.net,1433;Initial Catalog=lighthouseFSQ;Persist Security Info=False;User ID={Username};Password={Password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Any help is greatly appreciated. FYI I did try this in Windows VS2015 and it worked fine.
Upvotes: 2
Views: 2096
Reputation: 64307
By default, the dotnet new -t web
tempalte uses SQLite database, so only changing the connection string is not sufficient to change the provider.
You also need to remove the Microsoft.EntityFrameworkCore.Sqlite
package from project.json
and replace it with Microsoft.EntityFramework.SqlServer
(assuming you are using the SQLServer of Azure). Then in your Startup.cs
you need to replace the
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
with
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
and change the namespaces if necessary.
Update
In project.json you just need to change the dependency with Sqlite.
"dependencies": {
...
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",
...
}
}
to
"dependencies": {
...
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
...
}
}
If there is also a Microsoft.EntityFrameworkCore.Sqlite.Design
ch ange that too to Microsoft.EntityFrameworkCore.SqlServer.Design
too, it's there for scaffolding etc.
Upvotes: 4