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

Dynamic Queries seem to lose "Include()"s #28

Closed
pryankster opened this issue Jun 22, 2016 · 3 comments
Closed

Dynamic Queries seem to lose "Include()"s #28

pryankster opened this issue Jun 22, 2016 · 3 comments
Assignees

Comments

@pryankster
Copy link

// relevant bits of entities...
class Company {
    public string name { get; set; }
}

class Show {
    public string title { get; set; }
    public Company company { get; set; }
}

"search" is a string, posted from a web page.

// this works ... 
var qTotal = _context.Shows.Include( e => e.company ).Include( ... others ... );
qTotal = qTotal.Where( e => e.title.Contains(search) || e.company.name.Contains(search) );

// this throws null exception
var qTotal = _context.Shows.Include( e => e.company ).Include( ... others ... );
qTotal = qTotal.Where("title.Contains(@0) || company.name.Contains(@0)", search);
System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<SkipIterator>d__30`1.MoveNext()
   at System.Linq.Enumerable.<TakeIterator>d__24`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

From the VS2015 debug window:

Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [it].[id], [it].[companyid], [it].[slug], [it].[subtitle], [it].[timestamp], [it].[title], [it.company].[id], [it.company].[addressid], [it.company].[name], [it.company].[slug], [it.company].[timestamp], [p].[id], [p].[addressid], [p].[name], [p].[slug], [p].[timestamp]
FROM [Shows] AS [it]
LEFT JOIN [ProductionCompany] AS [it.company] ON [it].[companyid] = [it.company].[id]
LEFT JOIN [ProductionCompany] AS [p] ON [it].[companyid] = [p].[id]
ORDER BY [it].[companyid], [it].[id]
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [s].[id], [s].[ownerid], [s].[permission], [s].[role], [s].[timestamp], [s].[userId]
FROM [Staff<Show>] AS [s]
INNER JOIN (
    SELECT DISTINCT [it].[companyid], [it].[id]
    FROM [Shows] AS [it]
    LEFT JOIN [ProductionCompany] AS [it.company] ON [it].[companyid] = [it.company].[id]
) AS [it1] ON [s].[ownerid] = [it1].[id]
ORDER BY [it1].[companyid], [it1].[id]
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory: Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [e].[id], [e].[ProductionCompanyid], [e].[allDayEvent], [e].[description], [e].[duration], [e].[masterRecurid], [e].[notes], [e].[recurState], [e].[showid], [e].[start], [e].[timestamp], [e].[venueid]
FROM [Events] AS [e]
INNER JOIN (
    SELECT DISTINCT [it].[companyid], [it].[id]
    FROM [Shows] AS [it]
    LEFT JOIN [ProductionCompany] AS [it.company] ON [it].[companyid] = [it.company].[id]
) AS [it0] ON [e].[showid] = [it0].[id]
ORDER BY [it0].[companyid], [it0].[id]
Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory: Error: An exception occurred in the database while iterating the results of a query.
System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
Exception thrown: 'System.NullReferenceException' in Microsoft.EntityFrameworkCore.dll
@StefH StefH self-assigned this Jun 22, 2016
@pryankster
Copy link
Author

After a closer look, I'm closing this as "my bad" ... the problem I have encountered appears to be EF7 related. The null was from a nullable field having a null value, but the query was not being translated into SQL, it was being processed post-query.. going over to the EF repo to bug them!

@StefH
Copy link
Collaborator

StefH commented Jun 22, 2016

Maybe this one?
dotnet/efcore#4968

@pryankster
Copy link
Author

No, Root cause is this one: dotnet/efcore#5838

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants