实体框架核心将SQL Date误认为字符串

时间:2017-07-31 19:30:44

标签: c# sql-server entity-framework portable-class-library .net-standard

我在Xamarin PCL应用程序中使用Entity Framework Core来处理远程SQL数据库。其中一个数据库字段是可空的Date。我已经为表生成了一个模型,这是使用的属性:

public Nullable<System.DateTime> LastUpdated { get; set; }

日期的格式如表所示:2009-04-22

无论出于何种原因,当从表中加载对象时,它会抛出一个错误,指出它需要DateTime?但收到string而不是:

System.InvalidOperationException: An exception occured while reading a database value for property 'Item.LastUpdated'. The expected type was 'System.Nullable`1[System.DateTime]' but the actual value was of type 'System.String'.

我的猜测是它与格式化日期的方式有关,但我不确定。

为什么会发生这种情况,我该如何解决?

编辑: StackTrace

    at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.ThrowReadValueException[TValue] (System.Exception exception, System.Object value, Microsoft.EntityFrameworkCore.Metadata.IPropertyBase property) [0x0008f] in <2645c1c7345546f5b968e91de81e2b12>:0 
  at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityMaterializerSource.TryReadValue[TValue] (Microsoft.EntityFrameworkCore.Storage.ValueBuffer valueBuffer, System.Int32 index, Microsoft.EntityFrameworkCore.Metadata.IPropertyBase property) [0x00012] in <2645c1c7345546f5b968e91de81e2b12>:0 
  at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.EntityFrameworkCore.Storage.ValueBuffer)
  at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper`1[TEntity].Shape (Microsoft.EntityFrameworkCore.Query.QueryContext queryContext, Microsoft.EntityFrameworkCore.Storage.ValueBuffer valueBuffer) [0x0003e] in <1ce4ae811dd94fe5a5e10e630f2f4c7a>:0 
  at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider+<_ShapedQuery>d__3`1[T].MoveNext () [0x0005c] in <1ce4ae811dd94fe5a5e10e630f2f4c7a>:0 
  at System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Boolean& found) [0x00045] in <cc41d889a5b24501ae882a5cc55746dd>:0 
  at System.Linq.Enumerable.FirstOrDefault[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <cc41d889a5b24501ae882a5cc55746dd>:0 
  at (wrapper dynamic-method) System.Object:lambda_method (System.Runtime.CompilerServices.Closure,Microsoft.EntityFrameworkCore.Query.QueryContext)
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler+<>c__DisplayClass20_0`1[TResult].<CompileQueryCore>b__0 (Microsoft.EntityFrameworkCore.Query.QueryContext qc) [0x00074] in <2645c1c7345546f5b968e91de81e2b12>:0 
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult] (System.Linq.Expressions.Expression query) [0x00029] in <2645c1c7345546f5b968e91de81e2b12>:0 
  at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult] (System.Linq.Expressions.Expression expression) [0x00000] in <2645c1c7345546f5b968e91de81e2b12>:0 
  at System.Linq.Queryable.FirstOrDefault[TSource] (System.Linq.IQueryable`1[T] source) [0x0002f] in <cc41d889a5b24501ae882a5cc55746dd>:0 
  at BMobile.Pages.LoginPage.PerformLogin (System.Object sender, System.EventArgs e) [0x0003a] in C:\Users\rwilde\documents\visual studio 2017\Projects\BMobile\BMobile\BMobile\Pages\LoginPage.cs:44 

目标网站

{Void ThrowReadValueException [Nullable`1](System.Exception,System.Object,Microsoft.EntityFrameworkCore.Metadata.IPropertyBase)}

0 个答案:

没有答案