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

Crash when used in HostedService #2505

Open
montella1507 opened this issue Jun 26, 2019 · 4 comments

Comments

@montella1507
Copy link

commented Jun 26, 2019

Steps to reproduce

  1. Follow common steps for HostedService here
    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2&tabs=visual-studio#consuming-a-scoped-service-in-a-background-task

  2. Register EFCore with UseNpgsl()
    services.AddDbContext(opt =>
    { opt.UseNpgsql(connString);
    });

  3. In hostedService DoWork , call GetService() ;

The issue

EntityFramework works as expected in DoWork. However it Crashes EVERYWHERE else

ArgumentNullException: Value cannot be null. Parameter name: entityType
Microsoft.EntityFrameworkCore.Utilities.Check.NotNull<T>(T value, string parameterName)
Microsoft.EntityFrameworkCore.RelationalMetadataExtensions.Relational(IEntityType entityType)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression)
System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)
System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, bool inProjection)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
Remotion.Linq.Clauses.MainFromClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSubQueryModel(QueryModel queryModel)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.SqlTranslatingExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionVisitors.NpgsqlSqlTranslatingExpressionVisitor.VisitSubQuery(SubQueryExpression expression) in NpgsqlSqlTranslatingExpressionVisitor.cs

Further technical details

EFCore: 2.1.1
Npgsql version: 2.1.1
PostgreSQL version: 11 alpine
Operating system: Docker Linux

What have i tried and havent worked:

  • all transient scopes + CreateScope
  • do not create scope, just inject Transient Db context

Note:
If i create DBContext manually e.g (new DbContext()) it works, how ever i dont know how to use Npgsql via Constructor.

@YohDeadfall

This comment has been minimized.

Copy link
Member

commented Jun 26, 2019

Probably it should be asked in the EF Core repo since the exception raised in its internals. Could you verify that this works with SQL Server or SQLite? Just register and try to resolve a DbContext with one of these providers. If the problem exists then it's definitely an EF Core bug.

/cc @ajcvickers

@YohDeadfall YohDeadfall added the bug label Jun 26, 2019

@montella1507

This comment has been minimized.

Copy link
Author

commented Jun 26, 2019

Without NPGSQL it works correctly

Exception is thrown inside NpgsqlSqlTranslatingExpressionVisitor.cs

@roji

This comment has been minimized.

Copy link
Member

commented Jun 26, 2019

@montella1507 can you please submit a full minimal code sample? We need to see what query you're trying to run, your model, etc.

@montella1507

This comment has been minimized.

Copy link
Author

commented Jun 28, 2019

Deadlines incoming at the moment. Will provide in days.

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