efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Complex Type causes wrong sql generation with UseRowNumberForPaging

Open mehmetcankamar opened this issue 9 years ago • 1 comments

When we target our EF Core to use on SQL Server 2008, we use UseRowNumberForPaging as a parameter for SqlServerDbContextOptionsBuilder. It works great untill we use complex type.

Exception message: The column 'Id' was specified multiple times for 't'. Stack trace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Linq.Enumerable.SelectEnumerableIterator2.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__172.MoveNext() at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List1.AddEnumerable(IEnumerable1 enumerable) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at EFCoreBug.Program.LoadTestDataWithPaging() in c:\users\mehmet.kamar\documents\visual studio 2017\Projects\EFCoreBug\EFCoreBug\Program.cs:line 54 at EFCoreBug.Program.Main(String[] args) in c:\users\mehmet.kamar\documents\visual studio 2017\Projects\EFCoreBug\EFCoreBug\Program.cs:line 18

  public class Note
    {
        [Key]
        public Guid Id { get; set; }
        public string Text { get; set; }
        public User User { get; set; }
    }
    public class User
    {
        public string Fullname { get; set; }
        public string Email { get; set; }
    }

This method is not working as expected.

       static List<Note> LoadTestDataWithPaging()
        {
            using (NoteDbContext context = new NoteDbContext())
            {
                var queryableObj =  context.Note.Where(x => x.Text == "Foo Bar").AsQueryable();
                return queryableObj.Skip(0).Take(100).ToList();
            }
        }

Below is generated sql

  SELECT [t].[Id], [t].[Text], [t].[User_Email], [t].[User_Fullname]
FROM (
    SELECT [x].[Id], [x].[Text], [x].[Id], [x].[User_Email], [x].[User_Fullname], ROW_NUMBER() OVER(ORDER BY @@RowCount) AS [__RowNumber__]
    FROM [Note] AS [x]
    WHERE [x].[Text] = N'Foo Bar'
) AS [t]
WHERE ([t].[__RowNumber__] > 0) AND ([t].[__RowNumber__] <= (0 + 100))

EF Core version: 2.0.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10, Windows Server 2008 R2 IDE: (e.g. Visual Studio 2017)

I have also created a repository that produces the problem. https://github.com/mjohn/EFCoreBug

mehmetcankamar avatar Oct 26 '17 11:10 mehmetcankamar

@mjohn If you are still using RowNumberForPaging, could you please comment on issue #13959 and let us know why?

ajcvickers avatar Nov 14 '18 23:11 ajcvickers