I am trying to reverse create the C# code from an existing database.
If I manually enter my connection string and type the command:
Scaffold-DbContext 'Data Source=MyServer;Initial Catalog=MyCatalog;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False'
Microsoft.EntityFrameworkCore.SqlServer
-Tables DeviceParameterWithDevice -Context DPWDContext
-OutputDir Models -Force
This works OK, and I get all the correct code produced.
I add the same connection string into the appsettings.json
file (as well as the appsettings.Development.json
file - just in case)
{
"ConnectionStrings": {
"EnviroWatchDB": "Data Source=MyServer;Initial Catalog=MyCatalog;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
If I run
Scaffold-DbContext -connection name=EnviroWatchDB
Microsoft.EntityFrameworkCore.SqlServer
-Tables DeviceParameterWithDevice -Context DPWDContext
-OutputDir Models
I get the following errors
Unhandled exception. Unhandled exception. Unhandled exception. System.UriFormatException: Invalid URI: The hostname could not be parsed.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at System.Uri..ctor(String uriString, UriKind uriKind)
at System.Net.Http.HttpClient.CreateUri(String uri)
at System.Net.Http.HttpClient.GetAsync(String requestUri)
at Program.<>c__DisplayClass0_2.<<$>b__1>d.MoveNext() in Z:\MyDir\Program.cs:line 111
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() System.UriFormatException: Invalid URI: The hostname could not be parsed.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at System.Uri..ctor(String uriString, UriKind uriKind)
at System.Net.Http.HttpClient.CreateUri(String uri)
at System.Net.Http.HttpClient.GetAsync(String requestUri)
at Program.<>c__DisplayClass0_2.<<$>b__1>d.MoveNext() in Z:\MyDirProgram.cs:line 111
--- End of stack trace from previous location ---
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart() System.UriFormatException: Invalid URI: The hostname could not be parsed.
I have tried adding this to the program.cs
file
var connectionString = builder.Configuration.GetConnectionString("EnviroWatchDB");
builder.Services.AddDbContext<DbContext>(x => x.UseSqlServer(connectionString));
but no effect.
Any ideas what is happening?
Thanks
1条答案
按热度按时间kyks70gy1#
Your connection string in the
appsettings.json
is just simply invalid - you've included half the command line parameters for yourScaffold-DbContext
command, too....You need to change it to just this: