Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: AsNoTracking/AsTracking methods on Non-EF Queryable providers throws InvalidOperationException #7937

Closed
cdie opened this issue Mar 20, 2017 · 6 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@cdie
Copy link

cdie commented Mar 20, 2017

We've created an abstraction on top of EF that will be our main data access. When we try to moq it in order to retrieve test data to test business logic, this exception is thrown when returning a list as queryable.

System.InvalidOperationException : 'There is no method 'AsNoTracking' on type 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' that matches the specified arguments'

Note that it doesn't throw any exception in EF6 (same code with EF6 just works well)
Note also that .Include() doesn't throw anything

Steps to reproduce

Here's code to reproduce the issue

 class Program
    {

        static void Main(string[] args)
        {
            var data = new List<EntityTest>
            {
                new EntityTest{ Id=1, Value = "value1"},
                new EntityTest { Id = 1, Value = "value2" },
                new EntityTest { Id = 1, Value = "value3" }
            };
            var repoMoq = new Mock<Repository>();
            repoMoq.Setup(r => r.Get()).Returns(data.AsQueryable());

            var b = new Business(repoMoq.Object);

            Console.WriteLine(string.Join(",", b.GetAll().Select(u => u.Value)));

            Console.ReadKey();
        }
    }

    public class Business
    {
        Repository _repo;

        public Business(Repository repo)
        {
            _repo = repo;
        }

        public IEnumerable<EntityTest> GetAll()
            => _repo.Get()
            .AsNoTracking() // Explodes here
            .ToList();
    }

    public interface Repository
    {
        IQueryable<EntityTest> Get();
    }

    public class EntityTest
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

Further technical details

EF Core version: 1.1.1
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system: Winodws 10
IDE: VS 2017 RTW

@cdie cdie changed the title System.InvalidOperationException : 'There is no method 'AsNoTracking' on type 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions' that matches the specified arguments' when trying to moq data System.InvalidOperationException when trying to moq data Mar 20, 2017
@cdie cdie changed the title System.InvalidOperationException when trying to moq data System.InvalidOperationException when trying to moq data and call AsNoTracking method Mar 20, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 20, 2017
@ajcvickers
Copy link
Contributor

@smitpatel to investigate, checking with @anpete who did some related changes in this area previously.

@colltoaction
Copy link
Contributor

@cdwaddell
Copy link

I came across this same issue today and ended up writing my own gated version of AsNoTracking() as a workaround.

@ajcvickers
Copy link
Contributor

Looks like Include/ThenInclude has the special handling, but AsNoTracking does not. Changing from "investigate" to bug. We should also make sure that there aren't other extension methods that need similar changes. (AsTracking is the obvious one. 😄)

@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 22, 2017
@smitpatel smitpatel changed the title System.InvalidOperationException when trying to moq data and call AsNoTracking method AsNoTracking/AsTracking methods on Non-EF Queryable providers throws InvalidOperationException Mar 22, 2017
@ajcvickers ajcvickers changed the title AsNoTracking/AsTracking methods on Non-EF Queryable providers throws InvalidOperationException Query: AsNoTracking/AsTracking methods on Non-EF Queryable providers throws InvalidOperationException May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@bassebaba
Copy link

@cdwaddell Would you be willing to share your workaround?

@cdwaddell
Copy link

@bassebaba sure. Basically, I did the same thing that the other IQueryable extensions did in a custom IQueryable extension.

  public static class QueryableExtensions
  {
    public static IQueryable<T> AsGatedNoTracking<T>(this IQueryable<T> source) where T : class
    {
      if (source.Provider is EntityQueryProvider)
        return source.AsNoTracking<T>();
      return source;
    }
  }

To use this, you change
myquery.AsNoTracking()
To
myquery.AsGatedNoTracking()

-- Daniel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants