Linq查询下方在EF Core(版本1.1.0和1.1.1)中抛出ArgumentException
,但未在EF6中引发。
MessageLogs.FirstOrDefault(o =>
o.UserMessage.GeneratedTime.Add(o.UserMessage.Duration) > DateTimeOffset.UtcNow);
ArgumentException:不能使用'System.Nullable`1 [System.TimeSpan]'类型为'System.DateTimeOffset Add(System.TimeSpan)'方法'System.TimeSpan'类型参数。
注意:异常消息与我翻译的完全不同。
但它可以毫无例外地用于单表查询。
UserMessages
.FirstOrDefault(o => o.GeneratedTime.Add(o.Duration) > DateTimeOffset.UtcNow);
有可能是虫子吗?或者,我该如何解决这个问题?
其他信息
数据库
Server=(localdb)\mssqllocaldb;Database=TestDatabase;Trusted_Connection=True;
类
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; }
}
生成的迁移文件
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);
});
}
生成的SQL架构
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]
堆栈跟踪
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