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: Throw if second context instance is used in single query #11101

Closed
johnnycamby opened this issue Feb 28, 2018 · 18 comments

Comments

Projects
None yet
6 participants
@johnnycamby
Copy link

commented Feb 28, 2018

I am working on a project where i am doing reverse engineering of my database to auto generate entities and DbContexts using EF Core Power Tools version 1.0.525 in visual studio 2017 Version 15.5, Microsoft.EntityFrameworkCore 2.0.1, Microsoft.EntityFrameworkCore.SqlServer .
The reverse engineering is successful, pretty clean auto generated code thumbs to ErikEJ. But when am using Linq to Sql queries that involve inner joins and left outer join. I always run into this exception The value can not be NULL. Parameter name: entityType

below is a sample piece of my code as cannot upload the whole project because of a company regulation and also it being a big.

var query = from bs in this.mDbContext.tblboxs
 join ld in this.pdbContext.tblDicts on bs.BID equals ld.ID 
join mbs in this.crmDbContext.tblStatuses on bs.ID equals mbs.Id into mbsGrps 
from mbs in mbsGrps.DefaultIfEmpty()
 select new XRecord {
     BsID_Archiv = bs.ID,
   };
 return await query.ToListAsync();

Remember all the EF-fluent Api mappings and configurations are done for me when i use ** EF Core Power Tools ** and it really pretty clean auto generated code.
More information

  • my tables don't have any relationship constraint(s) but the relationship is in the data stored in those tables and also i happen to have alots of nullable fields as the project is just used for loading data then later i do SSIS ETL operations.
@anpete

This comment has been minimized.

Copy link

commented Mar 1, 2018

@johnnycamby Please post the stack trace, thanks.

@johnnycamby

This comment has been minimized.

Copy link
Author

commented Mar 2, 2018

This is the my stack trace and sorry for any German parts if any but i hope you can still figure it out

at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.RelationalMetadataExtensions.Relational(IEntityType entityType) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression) at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CompileJoinClauseInnerSequenceExpression(JoinClause joinClause, QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileJoinClauseInnerSequenceExpression(JoinClause joinClause, QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index) at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index) at Remotion.Linq.Clauses.JoinClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index) at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1 bodyClauses, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
--- End of stack trace from the previous location where the exception was thrown ---
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, IDiagnosticsLogger1 logger, Type contextType) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_01.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Remotion.Linq.QueryableBase1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at x.xf.Dxv.DataServices.DataLayer.Repositories.RecordRepository.GetXRecords(XResourceParameters resourceParamters) in C:\x\xf\Dxv.DataServices\DataLayer\Repositories\RecordRepository.cs:Line 50.
at Dxv.DataServices.Apis.XRecordsController.GetXrecords(XResourceParameters resourceParameters) in C:\x\xf\Dxv.DataServices\Apis\XRecordsController.cs:Line 41.
bei lambda_method(Closure , Object , Object[] )
bei Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
bei Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()`

@ajcvickers

This comment has been minimized.

Copy link
Member

commented Mar 2, 2018

@johnnycamby This may have already been fixed. Could you try with our nightly builds--the feeds are listed here: https://github.com/aspnet/EntityFrameworkCore

If it still fails, then we will need more info in order to reproduce what you are seeing. I know you can't post your full project, but it would be ideal if you are able to create a simplified repro project containing just enough to make it fail. If not, then we may be able to make some progress with more info about the shapes of the entity types involved.

@johnnycamby

This comment has been minimized.

Copy link
Author

commented Mar 5, 2018

alright i have now posted a test console project that reproduces the same error, you may find here: https://github.com/devcodeprobs/EfCoreTesting

@ajcvickers ajcvickers added this to the 2.1.0 milestone Mar 5, 2018

@anpete

This comment has been minimized.

Copy link

commented Mar 6, 2018

@johnnycamby Can you confirm that you tried against a nightly build? They are available here: https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json

Thanks.

@johnnycamby

This comment has been minimized.

Copy link
Author

commented Mar 6, 2018

No i didn't because my visual studio doesn't support .NET Core 2.1 Preview 1 and my project is in production phase so switching from one version because of testing may be time cost effective.- But I download a Visual Studio 2017 Preview version this week and test it out

@anpete

This comment has been minimized.

Copy link

commented Mar 6, 2018

@johnnycamby No problem, I can try it out if it's a hassle. We think there is a decent chance this is already fixed.

@johnnycamby

This comment has been minimized.

Copy link
Author

commented Mar 7, 2018

@anpete i think the problem is with using linq to sql inner and outer join queries on multiple different DbContexts. Does EF-Core support it.

@anpete

This comment has been minimized.

Copy link

commented Mar 8, 2018

@johnnycamby Mixing different contexts in a single query is not supported right now. It would be great to get an idea of your expectation for this query. I.e.

  • The contexts target the same database, and you expect a single SQL query to be produced. Or,
  • You expect multiple queries to be combined, client-side.

Thanks.

@johnnycamby

This comment has been minimized.

Copy link
Author

commented Mar 12, 2018

@anpete sorry for the late reply. My contexts were targeting different databases and yes i realised EF doesn't support it.

@anpete anpete removed this from the 2.1.0 milestone Mar 14, 2018

@ajcvickers

This comment has been minimized.

Copy link
Member

commented Mar 16, 2018

Discussed in triage, and for now we will throw if a second context instance is encountered, regardless of whether it is the same type or not.

@ajcvickers ajcvickers added this to the 2.1.0 milestone Mar 16, 2018

@ajcvickers ajcvickers added the type-bug label Mar 16, 2018

@anpete

This comment has been minimized.

Copy link

commented Mar 27, 2018

@ajcvickers @divega Did we consider the breaking-ness of this? Specifically, queries that uses two instances of the same context type will no longer work.

@divega

This comment has been minimized.

Copy link
Member

commented Mar 28, 2018

@anpete can’t speak for @ajcvickers but I had it in mind when we discussed it and still thought it is reasonable to start throwing. It seems it shouldn’t be that common (unless I am missing something) to do this by accident, and those who are doing it on purpose are likely not getting what they want. Of course it would be ideal to be able to combine multiple data sources like this and I hope in the long run we will support it.

@ajcvickers

This comment has been minimized.

Copy link
Member

commented Mar 28, 2018

Agree with @divega.

@divega divega modified the milestones: 2.1.0-preview2, 2.1.0 Apr 2, 2018

anpete added a commit that referenced this issue Apr 6, 2018

Fix #11101 - The value can not be NULL. Parameter name: entityType
- Validate that a query only uses a single EF IQueryProvider instance.

anpete added a commit that referenced this issue Apr 6, 2018

Fix #11101 - The value can not be NULL. Parameter name: entityType
- Validate that a query only uses a single EF IQueryProvider instance.

@anpete anpete closed this in ae91664 Apr 9, 2018

@divega divega added the closed-fixed label Apr 11, 2018

@ajcvickers ajcvickers changed the title The value can not be NULL. Parameter name: entityType Query: Throw is second context instance is used in single query May 17, 2018

@jingliancui

This comment has been minimized.

Copy link

commented May 28, 2018

Hello , so this issue was fixed? It's still shown in efcore 2.0.3

@smitpatel

This comment has been minimized.

Copy link
Contributor

commented May 29, 2018

@jingliancui - This issue has been fixed in 2.1.0 release. Hence you would still see error in 2.0.3

@jingliancui

This comment has been minimized.

Copy link

commented May 31, 2018

@smitpatel No,that still not fixed in 2.1.0,today I had upgrade my project to asp.net core 2.1.
My database is sqlserver 2008 r2.

@smitpatel

This comment has been minimized.

Copy link
Contributor

commented May 31, 2018

@jingliancui - Please file a new issue with repro code.

@ajcvickers ajcvickers changed the title Query: Throw is second context instance is used in single query Query: Throw if second context instance is used in single query May 31, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.