Hi.
I have a .NET 6.0 project with a DB context scaffolded from SQL DB and i was trying to generate a page with a blazor Grid. I all went fine but when i run the project i get the following error opening the page:
"System.InvalidOperationException: 'No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.'".
I believe the problem has to do with the context reference generated by code generator, specifacly with the instance bellow because the error occurs on the first line of the Read Method.
"TenantDbContext SfGridDataContext = new TenantDbContext();
/// <summary>
/// Method for read data from database
/// </summary>
public override object Read(DataManagerRequest dm, string key = null)
{
IEnumerable<Country> DataSource = SfGridDataContext.Countrys.ToList();"
My DbContext class is like this:
" public partial class TenantDbContext : DbContext
{
public TenantDbContext()
{
}
public TenantDbContext(DbContextOptions<TenantDbContext> options)
: base(options)
{
}
...protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
//optionsBuilder.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings["TenantConnection"].ConnectionString);
base.OnConfiguring(optionsBuilder);
}
}
}
Hi Carlos,
Thanks for your patience and sorry for the delay in getting back to you.
Based on your shared details we have checked the reported problem by generating the sample using the Blazor code generator. We have generated the grid page using the scaffolding way(by referring to the context file) and ensured the reported problem from our end. We could not able to reproduce the reported problem from our end. Find the below sample for your reference.
Note: Kindly change the connection string in OrderContext.cs file based on Northwnd.MDF file in App_data folder.
If you are still facing the problem, then could you please share the below details which will be helpful to validate and provide a better solution.
Regards,
Rahul
In the sample you provided you are using a service and not the DbContext directly.
But Syncfusion code generator extension does not generate a service but a DbContext like this
(Blazor page)
TenantDbContext SfGridDataContext = new TenantDbContext();
(Blazor DbContext)
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{ }
And the default constructor for the DbContext does not have an empty constructor but a constructor with " DbContextOptions<ApplicationDbContext> options" parameters. So the code returns an error because no empty constructor exists. If we add an empty constructor the connection doesn't work because it is empty.
Hi Carlos,
Thanks for sharing the details.
We are currently checking the reported query from our end and we will update the further details within two business days. Until then we appreciate your patience.
Regards,
Rahul
Hi Carlos,
Sorry for the delay in getting back to you.
We need some details regarding the reported case since we are quite unclear about the reported problem(how you are generating using scaffolding). Could you please share the below details which will be helpful to validate and provide a better solution?
Regards,
Rahul
Hi.
It took so long i just forgot about it.
What i use is your code generator component. I followed this help from your documentation
https://blazor.syncfusion.com/documentation/visual-studio-integration/code-generator
This code generator lets select an Entity Framework DbContext scaffolded from a Database with the command:
Scaffold-DbContext -Connection 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=ERP' -OutputDir Models\ERPDBContext -Context ERPDBContext -Provider Microsoft.EntityFrameworkCore.SqlServer
What i get is a DbContext class derived from EntityFrameworkCore DbContext like this
public partial class TenantDbContext : DbContext
{
public TenantDbContext(DbContextOptionsoptions)
: base(options)
{}
public virtual DbSetConcelhos { get; set; } = null!;
.....
Hi Carlos,
Thanks for the update.
We are currently checking the reported query from our end and we will update the further details within three business days. Until then we appreciate your patience.
Regards,
Rahul
Figured it out.
Must keep OnConfiguring method in the DbContext adapter like this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
var connectionString = configuration.GetConnectionString("TenantDBConnection");
optionsBuilder.UseSqlServer(connectionString);
}
Usually we follow Microsoft's indication for .NET Core and comment this and add the DbContext in services in program.cs. But using your dataManager requires this method to be available.
Hi Carlos,
Thanks for the update.
We are glad to hear that you have resolved your query on your own.
Kindly get back to us if you have further queries.
Regards,
Vignesh Natarajan