How to setup an execution strategy for Oracle in EF Core 2.2?

.net-core entity-framework-core

Question

I want to set up the execution strategy for Oracle in Entity Framework Core 2.2 by using the DbContext injection. I am using NuGet package Oracle.EntityFrameworkCore for connecting to an Oracle DB.

services.AddDbContext<MyContext>(options =>
        {
            options.UseOracle(configuration.GetConnectionString("MyConnection"), oracleOptionsAction: oracleOptions =>
            {
                oracleOptions.ExecutionStrategy(x => 
                    new MyExecutionStrategy();
            });
        });

I have created the following execution strategy class for testing purposes. It turns out the class DbExecutionStrategy does not exist in dotnet core:

public class MyExecutionStrategy : DbExecutionStrategy
{
    public MyExecutionStrategy() : base(10, TimeSpan.FromSeconds(30))
    {

    }

    protected override bool ShouldRetryOn(Exception exception)
    {
        return true;
    }
}
1
0
11/19/2019 6:57:36 PM

Popular Answer

The class DbExecutionStrategy does not exist in dotnet core only in dotnet. You must implement the abstract class ExecutionStrategy instead. The custom execution strategy class must be defined in this way:

public class MyExecutionStrategy : ExecutionStrategy
{
    public MyExecutionStrategy(ExecutionStrategyDependencies dependencies, int maxRetryCount, TimeSpan maxRetryDelay) 
        : base(dependencies, maxRetryCount, maxRetryDelay)
    {

    }

    protected override bool ShouldRetryOn(Exception exception)
    {        
        return true;
    }
}

Then the dependency injection section:

services.AddDbContext(options =>
{
    options.UseOracle(configuration.GetConnectionString("MyConnection"), oracleOptionsAction: oracleOptions =>
    {
        oracleOptions.ExecutionStrategy(dependencies => 
            new MyExecutionStrategy(
            dependencies,
            retryCount,
            retryDelay));
    });
});
1
11/19/2019 7:16:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow