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: Subquery.contains generates invalid SQL #7666

Closed
CurlerRoo opened this issue Feb 21, 2017 · 5 comments
Closed

Query: Subquery.contains generates invalid SQL #7666

CurlerRoo opened this issue Feb 21, 2017 · 5 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

@CurlerRoo
Copy link

CurlerRoo commented Feb 21, 2017

Exception:

System.Data.SqlClient.SqlException: Invalid column name 'PostId'.

Steps to reproduce

    public class Post
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual List<PostTag> PostsTags { get; set; }
    }

    public class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual List<PostTag> PostsTags { get; set; }

    }

    public class PostTag
    {
        public int PostId { get; set; }
        public int TagId { get; set; }
        public virtual Post Post { get; set; }
        public virtual Tag Tag { get; set; }
    }

    public class ApplicationDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<PostTag>().HasOne(m => m.Post).WithMany(m => m.PostsTags).HasForeignKey(m => m.PostId);
            builder.Entity<PostTag>().HasOne(m => m.Tag).WithMany(m => m.PostsTags).HasForeignKey(m => m.TagId);
            builder.Entity<PostTag>().HasKey(m => new { m.PostId, m.TagId });
        }

        protected override void OnConfiguring(DbContextOptionsBuilder builder)
        {
            builder.UseSqlServer(@"TODO: ADD CONNECTION STRING");
        }

        public DbSet<Post> Posts { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<PostTag> PostsTags { get; set; }
    }

    public static class DbInitializer
    {
        public static void Initialize(ApplicationDbContext context)
        {
            context.Database.EnsureCreated();
        }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var db = new ApplicationDbContext();
            DbInitializer.Initialize(db);
            
            var postIds = db.PostsTags.Include(m => m.Post).Where(m => m.Tag.Name == "www").Select(m => m.PostId);
            var posts = db.Posts.Where(m => postIds.Contains(m.Id)).ToList();

            db.SaveChanges();
        }
    }

Further technical details

EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2015

@rowanmiller
Copy link
Contributor

Could you post the SQL that is getting sent to the database?

@CurlerRoo
Copy link
Author

@rowanmiller
The generated SQL:

SELECT [m].[Id], [m].[Name]
      FROM [Posts] AS [m]
      WHERE [m].[Id] IN (
          SELECT [m.Tag].[PostId] -- Expects [m.Tag].[Id] instead of [m.Tag].[PostId]
          FROM [PostsTags] AS [m1]
          INNER JOIN [Tags] AS [m.Tag] ON [m1].[TagId] = [m.Tag].[Id]
          WHERE [m.Tag].[Name] = N'www'
      )

Expected:

SELECT [m].[Id], [m].[Name]
      FROM [Posts] AS [m]
      WHERE [m].[Id] IN (
          SELECT [m.Tag].[Id]
          FROM [PostsTags] AS [m1]
          INNER JOIN [Tags] AS [m.Tag] ON [m1].[TagId] = [m.Tag].[Id]
          WHERE [m.Tag].[Name] = N'www'
      )

@smitpatel
Copy link
Contributor

Shouldn't expected be

SELECT [m].[Id], [m].[Name]
      FROM [Posts] AS [m]
      WHERE [m].[Id] IN (
          SELECT [m1].[PostId]
          FROM [PostsTags] AS [m1]
          INNER JOIN [Tags] AS [m.Tag] ON [m1].[TagId] = [m.Tag].[Id]
          WHERE [m.Tag].[Name] = N'www'
      )

@smitpatel smitpatel self-assigned this Feb 23, 2017
@CurlerRoo
Copy link
Author

@smitpatel You are right. Thanks for correcting me.

@smitpatel
Copy link
Contributor

#7691 fixes this. 😌

@rowanmiller rowanmiller added this to the 2.0.0 milestone Feb 24, 2017
@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 Feb 25, 2017
@smitpatel smitpatel changed the title EF Core generates incorrect SQL Query: Subquery.contains generates invalid SQL Feb 25, 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
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
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

5 participants