efcore: The multi-part identifier "XXX" could not be bound. after Take() in EF Core 3.1.0

i saw this issue fixed … but i still it in EF Core 3.1.0

The following code :


   Context.Users.
                    Where(captain => captain.Id == CaptainId).
                    Select(captain => new CaptainDetailsDto()
                    {


                        TaxiCount = TaxiCount,
                        Rate = Rate,
                        IsStop = IsStop,

                        CaptainDto = new CaptainDto() {
                            Id = CaptainId,
                            UserName = captain.UserName,
                          

                            SerialNumber = captain.SerialNumber,
                            FullName = captain.FullName,
                            SureName = captain.SureName,
                            MobileNumber = captain.MobileNumber,
                            DateOfBirth = captain.DateOfBirth,
                            NationalNumber = captain.NationalNumber,
                            AddressHome = captain.AddressHome,
                            CityId = captain.CityId,
                            PathPhotoCaptain = PhotoPath, 


                            CaptainCar = captain.Cars.Where(car => !car.DateDeleted.HasValue).Select(car => new CaptainCarDto()
                            {
                                LabelCar = car.LabelCar,
                                NumCard = car.NumCard,
                                CarLicenceType = car.CarLicenceType,
                                CarModelId = car.CarModelId,  
                                CarTypeId = car.CarTypeId,
                                DateOutCarLicence = car.DateOutCarLicence,
                                DateOutCarSecurta = car.DateOutCarSecurta,
                                Color = car.Color,
                                NoteCar = car.Note,
                                PathPhotoCar = car.Documents.
                                             FirstOrDefault(document => !document.DateDeleted.HasValue && document.Type == (int)DocumentTypes.CarPhoto).Path.OriginalString,
                            }).FirstOrDefault(),

                            Salary = captain.Sallaries.FirstOrDefault(sallary => !sallary.DateDeleted.HasValue && !sallary.DateChange.HasValue).Value,



                            PercentTaxi = captain.PercentTaxi,
                            InsuranceValue = captain.InsuranceValue,

                            PathFileCaptain = captain.Documents.
                                        FirstOrDefault(document0 => !document0.DateDeleted.HasValue && document0.Type == (int)DocumentTypes.UserFile).Path.OriginalString,

                            DateRegister = DateTime.Now,
                            //LastSeen

                            NoteCaptain = captain.Note,
                        },


                     
                        CaptainRateDtos = captain.Cars.Where(car0 => !car0.DateDeleted.HasValue). 
                                   SelectMany(car0 => car0.Taxis).
                                   Where(taxi => !taxi.DateDeleted.HasValue && (taxi.UserRate.HasValue || (taxi.UserTextRate != ""))).
                                   OrderByDescending(taxi => taxi.DateArrived ?? (taxi.DateCaptainArrived ?? taxi.DateRequest)).Take(12).
                                   Select(taxi => new CaptainRateDto()
                                   {
                                       Rate = taxi.UserRate.Value,
                                       UserRateText = taxi.UserTextRate,
                                       DateRate = taxi.DateArrived ?? (taxi.DateCaptainArrived ?? taxi.DateRequest),
                                       Name = taxi.UserEUser.FullName + " " + taxi.UserEUser.SureName,
                                       UserId = taxi.UserEUser.Id,
                                       PathUserPhoto = taxi.UserEUser.Documents.
                                           FirstOrDefault(document1 => !document1.DateDeleted.HasValue && document1.Type == (int)DocumentTypes.UserPhoto).Path.OriginalString
                                   }).ToList(),

                        ReportCount = captain.Cars.Where(car1 => !car1.DateDeleted.HasValue).
                                SelectMany(car1 => car1.Taxis).Count(taxi0 => !taxi0.DateDeleted.HasValue && taxi0.ReportText != null && taxi0.ReportText != ""),
                    
                        OwnerValue = 0,

                    }).SingleOrDefault();

Exception:

The multi-part identifier “t.UserEUserId” could not be bound. The multi-part identifier “t.UserRate” could not be bound. The multi-part identifier “t.UserTextRate” could not be bound. The multi-part identifier “t.DateArrived” could not be bound. The multi-part identifier “t.DateArrived” could not be bound. The multi-part identifier “t.DateCaptainArrived” could not be bound. The multi-part identifier “t.DateCaptainArrived” could not be bound. The multi-part identifier “t.DateRequest” could not be bound. The multi-part identifier “t.Id0” could not be bound. The multi-part identifier “t.Id” could not be bound. The multi-part identifier “t.c” could not be bound.

The Sql :

exec sp_executesql N'SELECT [t6].[UserName], [t6].[SerialNumber], [t6].[FullName], [t6].[SureName], [t6].[MobileNumber], [t6].[DateOfBirth], [t6].[NationalNumber], [t6].[AddressHome], [t6].[CityId], [t6].[LabelCar], [t6].[NumCard], [t6].[CarLicenceType], [t6].[CarModelId], [t6].[CarTypeId], [t6].[DateOutCarLicence], [t6].[DateOutCarSecurta], [t6].[Color], [t6].[Note], [t6].[Path], [t6].[c], [t6].[c0], [t6].[c1], [t6].[PercentTaxi], [t6].[InsuranceValue], [t6].[Path0], [t6].[c2], [t6].[c3], [t6].[Note0], [t6].[c4], [t6].[Id], [t11].[UserRate], [t11].[UserTextRate], [t11].[c], [t11].[c0], [t11].[Id], [t11].[Path], [t11].[c1], [t11].[Id0], [t11].[Id1]
FROM (
    SELECT TOP(2) [a].[UserName], [a].[SerialNumber], [a].[FullName], [a].[SureName], [a].[MobileNumber], [a].[DateOfBirth], [a].[NationalNumber], [a].[AddressHome], [a].[CityId], [t3].[LabelCar], [t3].[NumCard], [t3].[CarLicenceType], [t3].[CarModelId], [t3].[CarTypeId], [t3].[DateOutCarLicence], [t3].[DateOutCarSecurta], [t3].[Color], [t3].[Note], [t3].[Path], [t3].[c], [t3].[c0], (
        SELECT TOP(1) [s].[Value]
        FROM [Sallaries] AS [s]
        WHERE ([a].[Id] = [s].[EUserId]) AND ([s].[DateDeleted] IS NULL AND [s].[DateChange] IS NULL)) AS [c1], [a].[PercentTaxi], [a].[InsuranceValue], [t5].[Path] AS [Path0], [t5].[c] AS [c2], GETDATE() AS [c3], [a].[Note] AS [Note0], (
        SELECT COUNT(*)
        FROM [Cars] AS [c]
        INNER JOIN [Taxis] AS [t] ON [c].[Id] = [t].[CarId]
        WHERE (([a].[Id] = [c].[EUserId]) AND [c].[DateDeleted] IS NULL) AND (([t].[DateDeleted] IS NULL AND [t].[ReportText] IS NOT NULL) AND ([t].[ReportText] <> N''''))) AS [c4], [a].[Id]
    FROM [AspNetUsers] AS [a]
    LEFT JOIN (
        SELECT [t2].[LabelCar], [t2].[NumCard], [t2].[CarLicenceType], [t2].[CarModelId], [t2].[CarTypeId], [t2].[DateOutCarLicence], [t2].[DateOutCarSecurta], [t2].[Color], [t2].[Note], [t2].[Path], [t2].[c], [t2].[c0], [t2].[Id], [t2].[EUserId]
        FROM (
            SELECT [c0].[LabelCar], [c0].[NumCard], [c0].[CarLicenceType], [c0].[CarModelId], [c0].[CarTypeId], [c0].[DateOutCarLicence], [c0].[DateOutCarSecurta], [c0].[Color], [c0].[Note], [t1].[Path], [t1].[c], 1 AS [c0], [c0].[Id], [c0].[EUserId], ROW_NUMBER() OVER(PARTITION BY [c0].[EUserId] ORDER BY [c0].[Id]) AS [row]
            FROM [Cars] AS [c0]
            LEFT JOIN (
                SELECT [t0].[Path], [t0].[c], [t0].[Id], [t0].[CarId]
                FROM (
                    SELECT [d].[Path], 1 AS [c], [d].[Id], [d].[CarId], ROW_NUMBER() OVER(PARTITION BY [d].[CarId] ORDER BY [d].[Id]) AS [row]
                    FROM [Documents] AS [d]
                    WHERE [d].[DateDeleted] IS NULL AND ([d].[Type] = 1)
                ) AS [t0]
                WHERE [t0].[row] <= 1
            ) AS [t1] ON [c0].[Id] = [t1].[CarId]
            WHERE [c0].[DateDeleted] IS NULL
        ) AS [t2]
        WHERE [t2].[row] <= 1
    ) AS [t3] ON [a].[Id] = [t3].[EUserId]
    LEFT JOIN (
        SELECT [t4].[Path], [t4].[c], [t4].[Id], [t4].[EUserId]
        FROM (
            SELECT [d0].[Path], 1 AS [c], [d0].[Id], [d0].[EUserId], ROW_NUMBER() OVER(PARTITION BY [d0].[EUserId] ORDER BY [d0].[Id]) AS [row]
            FROM [Documents] AS [d0]
            WHERE [d0].[DateDeleted] IS NULL AND ([d0].[Type] = 3)
        ) AS [t4]
        WHERE [t4].[row] <= 1
    ) AS [t5] ON [a].[Id] = [t5].[EUserId]
    WHERE [a].[Id] = @__CaptainId_0
) AS [t6]
OUTER APPLY (
    SELECT [t].[UserRate], [t].[UserTextRate], COALESCE([t].[DateArrived], COALESCE([t].[DateCaptainArrived], [t].[DateRequest])) AS [c], ([a0].[FullName] + N'' '') + [a0].[SureName] AS [c0], [a0].[Id], [t10].[Path], [t10].[c] AS [c1], [t].[Id0], [t].[Id] AS [Id1], [t].[c] AS [c2]
    FROM (
        SELECT TOP(12) [t7].[Id], [t7].[BagValue], [t7].[BoxId], [t7].[CaptainRate], [t7].[CaptainTextRate], [t7].[CarId], [t7].[DateArrived], [t7].[DateBooked], [t7].[DateCaptainArrived], [t7].[DateDeleted], [t7].[DateRequest], [t7].[DisableText], [t7].[DiscountId], [t7].[Distance], [t7].[EndLatitude], [t7].[EndLongitude], [t7].[IsReportDeleted], [t7].[LocalLatitude], [t7].[LocalLongitude], [t7].[Number], [t7].[PercentTaxi], [t7].[ReportText], [t7].[RestValue], [t7].[StartLatitude], [t7].[StartLongitude], [t7].[Tax], [t7].[TaxiType], [t7].[Time], [t7].[UserEUserId], [t7].[UserRate], [t7].[UserTextRate], [t7].[Value], [c1].[Id] AS [Id0], COALESCE([t7].[DateArrived], COALESCE([t7].[DateCaptainArrived], [t7].[DateRequest])) AS [c]
        FROM [Cars] AS [c1]
        INNER JOIN [Taxis] AS [t7] ON [c1].[Id] = [t7].[CarId]
        WHERE (([t6].[Id] = [c1].[EUserId]) AND [c1].[DateDeleted] IS NULL) AND ([t7].[DateDeleted] IS NULL AND ([t7].[UserRate] IS NOT NULL OR (([t7].[UserTextRate] <> N'''') OR [t7].[UserTextRate] IS NULL)))
        ORDER BY COALESCE([t7].[DateArrived], COALESCE([t7].[DateCaptainArrived], [t7].[DateRequest])) DESC
    ) AS [t8]
    INNER JOIN [AspNetUsers] AS [a0] ON [t].[UserEUserId] = [a0].[Id]
    LEFT JOIN (
        SELECT [t9].[Path], [t9].[c], [t9].[Id], [t9].[EUserId]
        FROM (
            SELECT [d1].[Path], 1 AS [c], [d1].[Id], [d1].[EUserId], ROW_NUMBER() OVER(PARTITION BY [d1].[EUserId] ORDER BY [d1].[Id]) AS [row]
            FROM [Documents] AS [d1]
            WHERE [d1].[DateDeleted] IS NULL AND ([d1].[Type] = 0)
        ) AS [t9]
        WHERE [t9].[row] <= 1
    ) AS [t10] ON [a0].[Id] = [t10].[EUserId]
) AS [t11]
ORDER BY [t6].[Id], [t11].[c2] DESC, [t11].[Id0], [t11].[Id1], [t11].[Id]',N'@__CaptainId_0 int',@__CaptainId_0=11

The whole issue with CaptainRateDtos =aptain.Cars.Whe…

EF Core version: 3.1.0 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET Core 3.1) Operating system: IDE: (e.g. Visual Studio 2019 16.4.2)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, it is too risky for patch and also involves API breaks.

@MhozaifaA no worries. With the additional info you provided I was able to reproduce this issue. @smitpatel it repros on current master as well.