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

HasQueryFilter generates incorrect sql, causing "multi-part identifier could not be bound" #10283

Closed
ngbernard opened this issue Nov 14, 2017 · 3 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

@ngbernard
Copy link

ngbernard commented Nov 14, 2017

I have two classes, Plant and UserPlant. UserPlant is a lookup table that ties Users from another data source to Plant (so I'm not mapping User). Both inherit from a base class that provides some properties common to my data objects.

public class MyDataObject
    {
        public long? Id { get; set; }
        
        public DateTime? Created { get; set; }
        
        public DateTime? Edited { get; set; }

        public bool? Active { get; set; }
    }

public class Plant: MyDataObject
    {
        public string Name { get; set; }

        public List<UserPlant> UserPlants { get; set; }
    }

public class UserPlant: MyDataObject  
    {
        public long PlantId { get; set; }

        public long UserId { get; set; }

        public Plant Plant { get; set; }
    }

My mappings look like this:

modelBuilder.Entity<Plant>().HasKey(x=>x.Id);

modelBuilder.Entity<UserPlant>().HasKey(x => new { x.UserId, x.PlantId });
modelBuilder.Entity<UserPlant>().HasOne(x => x.Plant).WithMany(x => x.UserPlants).HasForeignKey(x=>x.PlantId);
modelBuilder.Entity<UserPlant>().HasQueryFilter(x => x.Active.HasValue && x.Active.Value);

When I run

_myDbContext.Plant.Include(p => p.UserPlants)

I get

Exception message:
 'The multi-part identifier "t.Id" could not be bound.'

This is the generated sql:

SELECT [p].[Id], [p].[Active], [p].[Created], [p].[Edited], [p].[Name]
FROM [Plant] AS [p]
ORDER BY [p].[Id]

SELECT [u].[UserId], [u].[PlantId], [u].[Active], [u].[Created], [u].[Edited], [u].[Id]
FROM [UserPlant] AS [u]
ORDER BY [t].[Id]

The 2nd query seems to be the problem, referencing an alias 't' that doesn't exist. However, if I remove the HasQueryFilter mapping, it works. What am I doing wrong?

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 7
IDE: Visual Studio 2017

@ngbernard ngbernard changed the title HasQueryFilter causing generates incorrect sql, causing "multi-part identifier could not be bound" HasQueryFilter generates incorrect sql, causing "multi-part identifier could not be bound" Nov 14, 2017
@smitpatel
Copy link
Member

@ngbernard - Thanks for reporting this. There is nothing wrong you are doing. This seem to be bug in EFCore. The important point here is x.Active.HasValue && x.Active.Value is not server translatable hence the collection include query gets client based join and things go wrong afterwards in query execution.

As a work-around can you try setting query as x.Active == true and see if it works?

@ngbernard
Copy link
Author

@smitpatel I tried it and that did work, thanks! I also just found another workaround: if I also add a similar HasQueryFilter to Plant, like so

modelBuilder.Entity<Plant>().HasQueryFilter(x => x.Active.HasValue && x.Active.Value);

for some reason EF produces correct sql. I meant to do that anyway so that's what I'm going with, but I'll keep your solution in mind for other cases.

@ajcvickers ajcvickers added this to the 2.1.0 milestone Nov 17, 2017
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
@maumar
Copy link
Contributor

maumar commented Mar 8, 2018

relinq now translates nullableProperty.HasValue to: nullableProperty != null, which we can fully translate. Closing

@maumar maumar closed this as completed Mar 8, 2018
@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 Mar 8, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
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

4 participants