我已经开始使用EF核心使用cosmosDB.Sql。 我有一个简单的模型
public class Transaction
{
public Guid id { get; set; }
public DateTime StartTime { get; set; }
public string Property1 { get; set; }
public Status Status { get; set; }
}
public enum Status
{
NotInitiated = 1,
InProgress = 2,
Completed = 3
}
我正在使用 Microsoft.EntityFrameworkCore.Cosmos.Sql:2.2.0-preview1-35029 Microsoft.EntityFrameworkCore:2.2.0-preview1-35029 (和其他相关的)nuget包
我能够将Transaction对象保存到cosmosdb集合中
//来自集合的json样本
{
"id": "20acc26b-ce07-4807-b630-91acf78e29d1",
"StartTime ": "0001-01-01T00:00:00",
"Property1 ": "value",
"Discriminator": "Transaction",
"Status": 1,
"_rid": ",
"_self": "",
"_etag": "",
"_attachments": "attachments/",
"_ts": ....
}
//结束
但是当我尝试使用以下语法使用cosmosdb上下文获取Transaction对象时
this.context.Transaction.Where(x => x.id == id).FirstOrDefault();
我得到了例外。
``` 异常消息:
No coercion operator is defined between types 'Newtonsoft.Json.Linq.JToken' and 'Status'(EnumType).
堆栈跟踪:
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
at Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Internal.ValueBufferFactoryFactory.CreateGetValueExpression(Expression jObjectExpression, IProperty property)
at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1 enumerable)
at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable`1 initializers)
at Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Internal.ValueBufferFactoryFactory.Create(IEntityType entityType)
at Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Internal.EntityShaper.CreateShaperLambda()
at Microsoft.EntityFrameworkCore.Cosmos.Sql.Query.Expressions.Internal.QueryShaperExpression.Reduce()
at System.Linq.Expressions.Expression.ReduceAndCheck()
at System.Linq.Expressions.Expression.ReduceExtensions()
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExtensionExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression expression)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda)
at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
at System.Linq.Expressions.Expression`1.Compile(Boolean preferInterpretation)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at WK.Drago.WebApi.Controllers.CosmosDBController.Get(Guid transactionId) in D:\SourceCode\Development\Drago\WK.Drago.WebApi\Controllers\CosmosDBController.cs:line 37
at lambda_method(Closure , Object )
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at System.Threading.Tasks.ValueTask`1.get_Result()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()
EF Core版本:2.2.0-preview1-35029 作业系统:Windows 10 Enterprise IDE :(例如Visual Studio 2017 15.7.5)