Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Execution Strategy Error in VS2017 and Entity framework Core 2.0

I'm reading "Building Web Applications with Visual Studio 2017" (by Philip Japikse, Kevin Grossnicklaus, and Ben Dewey) and am getting stuck. When trying to create a class for a custome execution strategy with Entity Framework Core I get error CR0246 "The type or namespace name 'ExecutionStrategyContext' could not be found (are you missing a using directive or assembly reference?)"

The text only states that only System and Microsoft.EntityFrameworkCore.Storage are required references. The EF Core 2.0 documentation seems to match the text but I cannot get the error to go away.

Note: The book uses Core and EF 1.1 whereas I am using 2.0. But I don't see anything in any documentation that hints at this being the issue. The class is:

using System;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore;

namespace SpyStore.DAL.EF
{
    public class MyExecutionStrategy : ExecutionStrategy
    {
        public MyExecutionStrategy(ExecutionStrategyContext context) :
            base(context, ExecutionStrategy.DefaultMaxRetryCount, ExecutionStrategy.DefaultMaxDelay)
        {

        }

        public MyExecutionStrategy(ExecutionStrategyContext context, int maxRetryCount, TimeSpan maxRetryDelay) :
            base(context, maxRetryCount, maxRetryDelay)
        {

        }

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

VS2017 is highlighting both instances of ExecutionStrategyContext as the issue. I've tried changing them to just 'ExecutionStrategy context' instead of 'ExecutionStrategyContext context' but I don't think this is what I want and I still get an error because my first parameter is context. Any help is appreciated! Thanks!

like image 835
Shane Blume Avatar asked Mar 09 '23 02:03

Shane Blume


1 Answers

I can't provide a documentation link because at this time the related EF Core API documentation is not updated yet, but in v2.0 the ExecutionStrategyContext class has been replaced with ExecutionStrategyDependencies and the ExecutionStrategy class now has the following constructors:

protected ExecutionStrategy(DbContext context, int maxRetryCount, TimeSpan maxRetryDelay);

protected ExecutionStrategy(ExecutionStrategyDependencies dependencies, int maxRetryCount, TimeSpan maxRetryDelay);

According to that, the updated sample should be something like this:

public class MyExecutionStrategy : ExecutionStrategy
{
    public MyExecutionStrategy(DbContext context) :
        this(context, DefaultMaxRetryCount, DefaultMaxDelay)
    {
    }

    public MyExecutionStrategy(DbContext context, int maxRetryCount, TimeSpan maxRetryDelay) :
        base(context, maxRetryCount, maxRetryDelay)
    {
    }

    public MyExecutionStrategy(ExecutionStrategyDependencies dependencies) :
        this(dependencies, DefaultMaxRetryCount, DefaultMaxDelay)
    {
    }

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

    protected override bool ShouldRetryOn(Exception exception)
    {
        return true;
    }
}
like image 198
Ivan Stoev Avatar answered Mar 25 '23 16:03

Ivan Stoev