Problems with MySQL EF Core
lk-code opened this issue ยท 6 comments
Hi,
Does the library also work with the EF Core version of MySQL?
I use this initialization:
string connectionString = config.GetConnectionString("Database")!;
services.AddDbContext<DatabaseContext>(options =>
{
options.UseMySQL(connectionString);
});
services.AddGenericRepository<DatabaseContext>();
services.AddQueryRepository<DatabaseContext>();
If I want to use the instance, the following problem occurs:
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.
If I use DatabaseContext directly, there are no problems.
Hello @lk-code , Thank you for reaching out here. Yes! It should work with MySQL as well. If you call services.AddGenericRepository<DatabaseContext>();
then you should not call services.AddQueryRepository<DatabaseContext>();
@lk-code Could you please provide a small repo so that I can reproduce the issue.
Hi @TanvirArjel
i've created a new repository with the relevant logic: https://github.com/lk-code/mysql-genericrepo
you can find the initialization in Program.cs
in Data/AuthUserRepository.cs you can set a breakpoint in the ctor. Then you can see that DatabaseContext is correctly initialized and has a connection to the server, while the IRepository displays an exception at the locations.
Thanks for your help and feel free to ask further questions :)
@lk-code It seems like you have version mismatch issue. Make sure that you are using the same version of EF Core and the MySQL provider.
i updated to newest versions and now i've no problems :D