实体框架在获取DateTime时引发异常

时间:2019-05-22 11:56:07

标签: c# entity-framework

我有ef core(在Postgres上)模型

    public class Price
    {
        public Price()
        {
        }

        public string OrderId { get; set; }

        public string Currency { get; set; }

        public decimal Amount { get; set; }

        public string Message { get; set; }

        public PriceType PriceType { get; set; } //  enum

        public DateTime SyncingOn { get; set; }

        [ConcurrencyCheck]
        public DateTimeOffset UpdatedOn { get; set; }
    }


    modelBuilder.Entity<Price>(z =>
            {
                z.ToTable(nameof(Price));
                z.HasKey(p => p.OrderId);
                z.Property(p => p.PriceType)
                 .IsRequired()
                 .HasConversion(v => v.ToString(),
                                v => (PriceType)Enum.Parse(typeof(PriceType), v));

            });

此模型具有一个DateTime类型的字段。如果我尝试选择该字段,则EF抛出异常

dbContext.Prices.ToList()
  

System.ArgumentNullException:值不能为null。   参数名称:方​​法      在System.Linq.Expressions.Expression.Call(MethodInfo方法,表达式arg0)      在Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource。<> c__DisplayClass7_0.b__2(<> f__AnonymousType1 2 <>h__TransparentIdentifier0) at System.Linq.Utilities.<>c__DisplayClass2_0 3.b__0(TSource x)      在System.Linq.Enumerable.SelectEnumerableIterator 2.MoveNext() at System.Collections.Generic.List 1.AddEnumerable(IEnumerable 1 enumerable) at System.Collections.Generic.List 1.InsertRange(Int32 index,IEnumerable 1 collection) at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.CreateMaterializeExpression(IEntityType entityType, Expression materializationExpression, Int32[] indexMap) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.MaterializerFactory.CreateMaterializer(IEntityType entityType, SelectExpression selectExpression, Func 3 projectionAdder,Dictionary 2& typeIndexMap) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.CreateShaper(Type elementType, IEntityType entityType, SelectExpression selectExpression) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType) 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.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, 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.CreateAsyncQueryExecutor[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func 1编译器处)处      在Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync [TResult](表达式查询)      Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable 1.System.Collections.Generic.IAsyncEnumerable<TResult>.GetEnumerator() at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable 1处,TAccumulate种子,Func 3 accumulator, Func 2结果选择器,CancellationToken取消令牌)

如果我将DateTime更改为DateTimeOffset,则效果很好

1 个答案:

答案 0 :(得分:0)

对不起,但这是我的错误。我们有自己的EntityMaterializerSource,它无法正常工作。我将关闭问题