EF Core DateTimeOffset and TimeSpan throws ArgumentException

Below Linq throws query ArgumentException

in EF Core (versions 1.1.0 and 1.1.1), but not raised in EF6.

MessageLogs.FirstOrDefault(o => 
               o.UserMessage.GeneratedTime.Add(o.UserMessage.Duration) > DateTimeOffset.UtcNow);

      

ArgumentException: Unable to use type System.Nullable1 [System.TimeSpan] as parameter of type System.DateTimeOffset Add (System.TimeSpan) 'System.TimeSpan'.

Note. The exception message is not exactly the same as mine.

But it works with a single query of the table without exception.

UserMessages
.FirstOrDefault(o => o.GeneratedTime.Add(o.Duration) > DateTimeOffset.UtcNow);

      

Is it possible? or how can i solve this?

Additional Information

Database

  • MSSQL Express 12.0 localdb: Server=(localdb)\mssqllocaldb;Database=TestDatabase;Trusted_Connection=True;

Classes

public class MessageLog
{
    public Guid Id { get; set; }
    public UserMessage UserMessage { get; set; }
}

public class UserMessage
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public TimeSpan Duration { get; set; }
    public DateTimeOffset GeneratedTime { get; set; }
}

      

Generated migration file

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "UserMessages",
        columns: table => new
        {
            Id = table.Column<Guid>(nullable: false),
            GeneratedTime = table.Column<DateTimeOffset>(nullable: false),
            Duration = table.Column<TimeSpan>(nullable: false),
            Text = table.Column<string>(nullable: true),
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_UserMessages", x => x.Id);
        });

    migrationBuilder.CreateTable(
        name: "MessageLogs",
        columns: table => new
        {
            Id = table.Column<Guid>(nullable: false),
            GeneratedTime = table.Column<DateTimeOffset>(nullable: false),
            UserMessageId = table.Column<Guid>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_MessageLogs", x => x.Id);
            table.ForeignKey(
                name: "FK_MessageLogs_UserMessages_UserMessageId",
                column: x => x.UserMessageId,
                principalTable: "UserMessages",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        });

}

      

Generated SQL schema

CREATE TABLE [dbo].[MessageLogs](
    [Id] [uniqueidentifier] NOT NULL,
    [UserMessageId] [uniqueidentifier] NULL,
 CONSTRAINT [PK_MessageLogs] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

ALTER TABLE [dbo].[MessageLogs]  WITH CHECK ADD  CONSTRAINT [FK_MessageLogs_UserMessages_UserMessageId] FOREIGN KEY([UserMessageId])
REFERENCES [dbo].[UserMessages] ([Id])
GO

ALTER TABLE [dbo].[MessageLogs] CHECK CONSTRAINT [FK_MessageLogs_UserMessages_UserMessageId]
GO


CREATE TABLE [dbo].[UserMessages](
    [Id] [uniqueidentifier] NOT NULL,
    [GeneratedTime] [datetimeoffset](7) NOT NULL,
    [Duration] [time](7) NOT NULL,
    [Text] [nvarchar](max) NULL,
 CONSTRAINT [PK_UserMessages] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

      

StackTrace

at: System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at: System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)
at: System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at: System.Linq.Expressions.MethodCallExpression.Update(Expression object, IEnumerable`1 arguments)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression node)
at: System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at: System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.VisitBinary(BinaryExpression node)
at: System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
at: System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.VisitBinary(BinaryExpression node)
at: System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
at: System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at: Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func`2 transformation)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ExpressionTransformingQueryModelVisitor`1.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.NavigationRewritingQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
at: Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
at: Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
at: Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at: Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.Rewrite(QueryModel queryModel, QueryModel parentQueryModel)
at: Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel)
at: Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
at: Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
at: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, ILogger logger, Type contextType)
at: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>b__0()
at: Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at: Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)
at: Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at: Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at: System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at: WabaraJobs_10MinuteQuestion.Functions.getRecentMessage(WabaraContext wabaraContext, IUserMessage msg) file C:\Users\Youngjae\Documents\Functions.cs:line 202

      

+3


source to share





All Articles