Skip to content

Incorrect table alias name in translated sql  #6636

@nextsummer33

Description

@nextsummer33

Steps to reproduce

Using Linq to generate sql running in sql server 2012.

var query = 
    from app in _context.CandidateApplications
    from ive in _context.Interviewees
        .Where(ive => ive.ApplicationId == app.CandidateApplicationId)
        .DefaultIfEmpty()
    from iv in _context.Interviews
        .Where(iv => iv.Id == ive.InterviewId)
        .DefaultIfEmpty()
    from ivr in _context.Interviewers
        .Where(ivr => ivr.InterviewId == ive.InterviewId)
        .DefaultIfEmpty()
    join s in _context.ApplicationStatuses on app.ApplicationStatusCode equals s.Code
    join c in _context.Users on app.CandidateId equals c.Id
    join j in _context.Jobs on app.JobId equals j.JobId
    join ip in _context.InternalPositions on j.PositionCode equals ip.Code
    join id in _context.InternalDepartments on j.InternalDepartmentCode equals id.Code
    select new {
        ApplicationId = app.CandidateApplicationId,
        ApplicationCode = app.ReferenceCode,
        AppliedDate = app.AppliedDate.Value,
        ApplicationStatusCode = app.ApplicationStatusCode,
        CandidateId = app.CandidateId,
        JobId = app.JobId,
        CandidateCode = c.ReferenceCode,
        FirstName = c.FirstName,
        LastName = c.LastName,
        JobCode = j.ReferenceCode,
        InternalPositionCode = j.PositionCode,
        InternalDepartmentCode = j.InternalDepartmentCode,
        InternalPosition = ip.Name,
        InternalDepartment = id.Name,
        ApplicationStatus = s.Status,
        InterviewId = iv.Id,
        InterviewDate = iv.InterviewDate,
        InterviewStartTime = ive.StartTime,
        InterviewEndTime = ive.EndTime,
        InterviewLocation = iv.Location,
        InterviewRemark = iv.Remark,
    };

The translated sql query:

SELECT [app].[CandidateApplicationId], [app].[ReferenceCode], [app].[AppliedDate], [app].[ApplicationStatusCode], [app].[CandidateId], [app].[JobId], [c].[ReferenceCode], [c].[FirstName], [c].[LastName], [j].[ReferenceCode], [j].[PositionCode], [j].[InternalDepartmentCode], [ip].[Name], [id].[Name], [s].[Status], [t3].[Id], [t3].[InterviewDate], [t1].[StartTime], [t1].[EndTime], [t3].[Location], [t3].[Remark]
      FROM [CandidateApplications] AS [app]
      CROSS APPLY (
          SELECT [t0].*
          FROM (
              SELECT NULL AS [empty]
          ) AS [empty0]
          LEFT JOIN (
              SELECT [ive0].*
              FROM [Interviewees] AS [ive0]
              WHERE [ive0].[ApplicationId] = [app].[CandidateApplicationId]
          ) AS [t] ON 1 = 1
      ) AS [t1]
      CROSS APPLY (
          SELECT [t20].*
          FROM (
              SELECT NULL AS [empty]
          ) AS [empty10]
          LEFT JOIN (
              SELECT [iv0].*
              FROM [Interviews] AS [iv0]
              WHERE [iv0].[Id] = [t1].[InterviewId]
          ) AS [t2] ON 1 = 1
      ) AS [t3]
      CROSS APPLY (
          SELECT [t40].*
          FROM (
              SELECT NULL AS [empty]
          ) AS [empty20]
          LEFT JOIN (
              SELECT [ivr0].*
              FROM [Interviewers] AS [ivr0]
              WHERE [ivr0].[InterviewId] = [t1].[InterviewId]
          ) AS [t4] ON 1 = 1
      ) AS [t5]
      INNER JOIN [ApplicationStatuses] AS [s] ON [app].[ApplicationStatusCode] = [s].[Code]
      INNER JOIN [Candidates] AS [c] ON [app].[CandidateId] = [c].[CandidateId]
      INNER JOIN [Jobs] AS [j] ON [app].[JobId] = [j].[JobId]
      INNER JOIN [InternalPositions] AS [ip] ON [j].[PositionCode] = [ip].[Code]
      INNER JOIN [InternalDepartments] AS [id] ON [j].[InternalDepartmentCode] = [id].[Code]

The issue

The alias name or table name is translated with a 0 at the end of name. It caused columns in the table could not be found.

  1. [t] => [t0]
  2. [t2] => [t20]
  3. [t4] => [t40]
fail: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[1]
      An exception occurred in the database while iterating the results of a query.
      System.Data.SqlClient.SqlException: The column prefix 't0' does not match with a table name or alias name used in the query.
      No column name was specified for column 1 of 't1'.
      Invalid column name 'InterviewId'.
      The column prefix 't20' does not match with a table name or alias name used in the query.
      No column name was specified for column 1 of 't3'.
      Invalid column name 'InterviewId'.
      The column prefix 't40' does not match with a table name or alias name used in the query.
      No column name was specified for column 1 of 't5'.
      Invalid column name 'Id'.
      Invalid column name 'InterviewDate'.
      Invalid column name 'StartTime'.
      Invalid column name 'EndTime'.
      Invalid column name 'Location'.
      Invalid column name 'Remark'.
         at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__107_0(Task`1 result)
         at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
         at System.Threading.Tasks.Task.Execute()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__20.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<MoveNext>d__8.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable`2.SelectAsyncEnumerator.<MoveNext>d__4.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable`2.SelectAsyncEnumerator.<MoveNext>d__4.MoveNext()
      --- End of stack trace from previous location where exception was thrown ---
         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
         at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
         at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()

Further technical details

EF Core version: "1.0.1"
Operating system: MacOS
Visual Studio version: n/a

Other details about my project setup:
Working with sql server 2012

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions