efcore icon indicating copy to clipboard operation
efcore copied to clipboard

VS fails to show proper intellisense when using multiple ThenIncludes

Open bigteejay opened this issue 10 years ago • 14 comments

See this SO post.

I was trying another .Include(blah).ThenInclude(blah), and while intellisense worked for the first, the second only showed IEnumberable functions/methods which lead me to believe I had mistyped something or it wasn't supported somehow.

For example...

var reg =  _context.Registrations
  .Where(r => r.User.UserName == userName)
  .Include(r => r.Activities).ThenInclude(a => a.Task)
  .Include(r => r.Activities).ThenInclude(a => a.Responses)
  .FirstOrDefault();

The second a in the second ThenInclude did not show this as being a valid property. Thanks to the contributor to my SO post I tried building and running it anyways and it worked fine. VS lead me astray! ;) Is this the proper place to report this?

VS Info...

Microsoft Visual Studio Professional 2015 Version 14.0.24720.00 Update 1 Microsoft .NET Framework Version 4.6.01055 Installed Version: Professional

EF info (as per project.json & "References" tree control in VS)...

EntityFramework.Core (7.0.0-rc1-final)

bigteejay avatar Dec 17 '15 10:12 bigteejay

Some more details...

Here are the two definitions of ThenInclude. The issue is that after calling .Include on a collection, IntelliSense uses the second overload.

public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;
public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(this IIncludableQueryable<TEntity, TPreviousProperty> source, Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class;

This is unfortunate because chaining after calling Include on a collection is the primary use case for ThenInclude. After a reference, you have the option of just dotting thru within the Include call.

rowanmiller avatar Jan 07 '16 20:01 rowanmiller

@divega to file an issue with VS

rowanmiller avatar Jan 15 '16 19:01 rowanmiller

Here is a self-contained repro (i.e. no EF in the picture but just API mockups):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ThenIncludeIntellisenseBug
{
    class Program
    {
        static void Main(string[] args)
        {
            var registrations = new List<Registration>().AsQueryable();

            var reg = registrations
              .Include(r => r.Activities).ThenInclude(a => a.Task);
              // type "a => a." and only ICollection<T> and Enumerable members appear
        }
    }

    internal class Registration
    {
        public ICollection<Activity> Activities { get; set; }
    }

    public class Activity
    {
        public Task Task { get; set; }
    }

    public class Task
    {
        public string Name { get; set; }
    }

    public interface IIncludableQueryable<out TEntity, out TProperty> : IQueryable<TEntity>
    {
    }

    public static class EntityFrameworkQuerybleExtensions
    {
        public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
         this IQueryable<TEntity> source,
          Expression<Func<TEntity, TProperty>> navigationPropertyPath)
         where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }

        public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }

        public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, TPreviousProperty> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }
    }
}

divega avatar Jan 22 '16 23:01 divega

This is now tracked as an Intellisense issue: https://github.com/dotnet/roslyn/issues/8237.

divega avatar Jan 29 '16 05:01 divega

Btw the I am facing the same issue on mono (omnisharp) on mac

ahmad-moussawi avatar Jan 30 '16 16:01 ahmad-moussawi

@ahmad-moussawi feel free to post this information to the Roslyn bug. It can help prioritizing the issue.

divega avatar Jan 31 '16 16:01 divega

Closing as this is an external issue in Roslyn

rowanmiller avatar Feb 01 '16 19:02 rowanmiller

@divega: @smitpatel is seeing this work correctly now. Can you confirm on your machine?

ajcvickers avatar Oct 09 '17 18:10 ajcvickers

@smitpatel I can still repro this on my machine, both with EF Core and with the fake code from https://github.com/dotnet/roslyn/issues/8237. I have Visual Studio 15.3.5, no R#. Any differences you can think of?

divega avatar Oct 09 '17 19:10 divega

@divega I can confirm that the issue is still active once again at Visual Studio 15.4.0 image

devtekve avatar Oct 29 '17 12:10 devtekve

Would it be an option to have two differently named methods instead of two overloads?

GeroIwan avatar Jul 12 '18 10:07 GeroIwan

This issue has been resolved in upcoming release of VS 16.1

smitpatel avatar Apr 24 '19 22:04 smitpatel

This issue has been resolved in upcoming release of VS 16.1

I'm with VS 16.3.9 and still see it.

The difference with the example from Roslyn issue is that there the collection overload is defined as

this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source

while in EF Core it is

this IIncludableQueryable<TEntity, IEnumerable<TPreviousProperty>> source

So when the navigation property type is ICollection<T> (or some other enumerable derived interface), the Intellisense does not offer T members initially. If the navigation property type is IEnumerable<T>, it works.

powermetal63 avatar Nov 20 '19 12:11 powermetal63

@powermetal63 - Please file an issue on roslyn repo.

smitpatel avatar Nov 20 '19 15:11 smitpatel