I have a .netcore 3.1 blazor app with EF Core 5 and after scaffolding my database with
Scaffold-DbContext "Name=MyDb" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDb_Context -Force
Everything works without issue if I use
Scaffold-DbContext "Server=servername;Database=MyDb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDb_Context -Force
But I want to avoid having the connection string in the source code.
everything scaffolds correctly, however, when I try to display data to a list, a get this error:
System.InvalidOperationException: A named connection string was used, but the name 'MyDb' was not found in the application's configuration. Note that named connection strings are only supported when using 'IConfiguration' and a service provider, such as in a typical ASP.NET Core application. See https://go.microsoft.com/fwlink/?linkid=850912 for more information.
Below is the code for my appsettings.json:
{
"ConnectionStrings": {
"MyDb": "Server=servername;Database=MyDb;Integrated Security=True"
}
}
Here is my MyDb_Context:
public partial class MyDb_Context : DbContext
{
public MyDb_Context()
{
}
public MyDb_Context(DbContextOptions<MyDb_Context> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Name=MyDb");
}
}
...
}
Here is my Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_env = env;
}
private readonly IWebHostEnvironment _env;
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var connection = Configuration.GetConnectionString("MyDb");
services.AddDbContext<MyDb_Context>(options => options.UseSqlServer(connection));
}
...
}
And here is what is being called in my service:
public class MyDbService
{
public readonly MyDb_Context mydb_context;
public MyDbService(MyDb_Context mydbctx)
{
MyDb_Context = mydbctx;
MyDb_Context.Database.SetCommandTimeout(60);
}
public Task<List<MyDbTableName>> GetMyDbContent()
{
var usernames = mydb_context.usernames.Where(u => u.AppId == 2).ToListAsync();
return usernames ;
}
...
}
Then in my Razor page I am using the following code:
@code{
private List<usernames> _usernames;
MyDbService mydbservice = new MyDbService(new MyDb_Context()); //I think this line is where it is breaking
protected override async Task OnInitializedAsync()
{
//Grab function from the service listed above
//Function:
_usernames = await mydbservice.GetMyDbContent();
}
}
I have no idea why its not finding the connectionstring, and I can even put a breakpoint on startup.cs on
optionsBuilder.UseSqlServer("Name=MyDb");
and it is able to read the connection string perfectly fine.
I also can see that DBContext gets loaded in when try
services.toList();
and breakpoint on it.
Please help, I have been at this for over a week and have exhausted my efforts.
question from:
https://stackoverflow.com/questions/65832644/error-finding-connection-string-in-blazor-net-core-app