Open Weijee opened 4 years ago
Probably you can find an answer faster asking on stack overflow.
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 object in its constructor and passes it to the base constructor for DbContext.'
If you use ASP.NET and register your DbContext
with services.AddDbContext<TestDbContext>()
, then you should use dependency injection to get an instance of your context.
So do not use code like the following:
public class MyController : ControllerBase
{
public async Task<ActionResult<int>> GetIceCreamCount()
{
// This will throw with your example code.
using (var dbContext = new TestDbContext())
{
var iceCreams = _dbContext.IceCreams.ToListAsync();
return iceCreams.Count;
}
}
}
Instead do something like the following:
public class MyController : ControllerBase
{
private readonly TestDbContext _dbContext;
// This constructor uses dependency injection.
public MyController(TestDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<ActionResult<int>> GetIceCreamCount()
{
var iceCreams = _dbContext.IceCreams.ToListAsync();
return iceCreams.Count;
}
}
You would also need to make sure, that you run the application for the processor architecture of the underlying database provider, which is likely to be 32 Bit for the usual Jet database driver.
To accomplish that in Visual Studio, you would need to make sure, that VS uses the correct version of dotnet, which can be done by adding the following lines to you project file (or something similar):
<PropertyGroup>
<RunCommand Condition="'$(PlatformTarget)' == 'x86'">$(MSBuildProgramFiles32)\dotnet\dotnet</RunCommand>
<RunCommand Condition="'$(PlatformTarget)' == 'x64'">$(ProgramW6432)\dotnet\dotnet</RunCommand>
</PropertyGroup>
Then set you platform target to x86
(either in the project file or in the project properties dialog).
The following program shows, how this would work (here without a controller):
In theory, this should all work.
I want to use efcore.jet 2.2.0 in my asp.netcore 2.2.0 project.
Unfortunately, the provider does not yet work with the .NET Core (or ASP.NET Core) runtime, mainly because it depends on System.Data.OleDb
from .NET Framework 4.
This is also stated on the main page of this repository:
This provider [...] does not work under the .NET Core runtime (netcore and netstandard).
Because of that, the provided sample code should not work with the .NET Core runtime at all.
The planned support for EF Core 3.1.0 will make use of the new System.Data.OleDb
.NET Core package and will fully work with .NET Core 3.1.
@bubibubi @lauxjpn Thank you for your explain。For now i am using System.Data.OleDb .NET Core package to access my MS Access.mdb, is there any release schedule of the JetEFCore3.1 now?
I plan to start working on the upgrade in February.
See #34 for further details.
I plan to start working on the upgrade in February.
See #34 for further details.
Thank you for your reply. Looking forward.
I want to use efcore.jet 2.2.0 in my asp.netcore 2.2.0 project. But I got a exception as below:
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 object in its constructor and passes it to the base constructor for DbContext.'
My Connection String is
"ConfigureServices" Method:
Any one can help me? Is there any Samples?