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

Includes() with string throws not finding navigation path exception when type T is derived #20609

Closed
vflame opened this issue Apr 12, 2020 · 1 comment · Fixed by #20630
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@vflame
Copy link

vflame commented Apr 12, 2020

Is it possible to have Includes() using strings not throw an exception when a derived type is passed in that doesn't have a specific navigation property? My use case is a generic method that will eager-load all navigation properties for one hierarchy. I'm using OfType() and having to set the DbSet to the base type as a workaround but it seems redundant since DbSet already defines the type and OfType should be unnecessary. Using Include with public navigation properties (e.g., Include(p=> ((ClassA)p).SubA)) works but these navigations are configured as private fields/private properties. This will likely go away when #2953 is resolved.

For example,

public class BaseClass
{
public string Id {get; set;}
}

public class ClassA : BaseClass
{
public SubA SubA {get;set;}
}

public class ClassB : BaseClass
{
public SubB SubB {get;set;}
}

public class ClassC : BaseClass
{
public SubA SubA {get;set;}
public SubC SubC {get;set;}
}

context.Set<T>().Include("SubA").Include("SubB").Include("SubC").ToList(); //exception thrown navigation not found when T is derived/concrete. Works if T is 'BaseClass'

context.Set<BaseClass>().Include("SubA").Include("SubB").Include("SubC").OfType<T>().ToList(); //works but the oftype call appears to be redundant and having to set the baseclass on Set<T>() weakens the use of generics

Further technical details

EF Core version: 5 preview 3 daily
Database provider: Mssqlocaldb
Target framework: .NET 5
Operating system: Win 10
IDE: Visual Studio 2019 Preview

@vflame vflame changed the title Includes() with string not finding navigation path when type T is derived Includes() with string throws not finding navigation path exception when type T is derived Apr 12, 2020
@maumar maumar self-assigned this Apr 13, 2020
maumar added a commit that referenced this issue Apr 14, 2020
…path exception when type T is derived

Fix is to use logging infrastructure for the "invalid include path" error. Log event is configured to throw by default but can be overriden in the dbcontextoptions. Event id is CoreEventId.InvalidIncludePathError
maumar added a commit that referenced this issue Apr 14, 2020
…path exception when type T is derived

Fix is to use logging infrastructure for the "invalid include path" error. Log event is configured to throw by default but can be overriden in the dbcontextoptions. Event id is CoreEventId.InvalidIncludePathError
maumar added a commit that referenced this issue Apr 14, 2020
…path exception when type T is derived

Fix is to use logging infrastructure for the "invalid include path" error. Log event is configured to throw by default but can be overriden in the dbcontextoptions. Event id is CoreEventId.InvalidIncludePathError
maumar added a commit that referenced this issue Apr 14, 2020
…path exception when type T is derived

Fix is to use logging infrastructure for the "invalid include path" error. Log event is configured to throw by default but can be overriden in the dbcontextoptions. Event id is CoreEventId.InvalidIncludePathError
maumar added a commit that referenced this issue Apr 17, 2020
…path exception when type T is derived

Fix is to use logging infrastructure for the "invalid include path" error. Log event is configured to throw by default but can be overriden in the dbcontextoptions. Event id is CoreEventId.InvalidIncludePathError
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 17, 2020
@maumar maumar added this to the 5.0.0 milestone Apr 17, 2020
@maumar
Copy link
Contributor

maumar commented Apr 17, 2020

converted the error to use logging infra instead of just throwing. The behavior can be configured so that the exception is not thrown when the error occurs (although by default it is still thrown).
event id: CoreEventId.InvalidIncludePathError

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. customer-reported type-bug
Projects
None yet
3 participants