gpt4 book ai didi

c# - 错误 : LINQ to Entities does not recognize the method DataLength

转载 作者:太空狗 更新时间:2023-10-29 21:27:33 26 4
gpt4 key购买 nike

我有一个简短的问题。首先使用 EF6 模型。

var db = new MyEntities(GetEntityConnectionString());

ObjectContext objectContext = ((IObjectContextAdapter)db).ObjectContext;
ObjectSet<DOCUMENT> objectSet = objectContext.CreateObjectSet<DOCUMENT>();
var results = objectSet.Where("SqlServer.DATALENGTH(it.BINARYCONTENT)>50000");
Assert.IsTrue(results.ToList().Count == 9);

var results2 = objectSet.Where(doc=>System.Data.Objects.SqlClient.SqlFunctions.DataLength( doc.BINARYCONTENT)>50000);
Assert.IsTrue(results2.ToList().Count == 9);

var results3 = db.DOCUMENTS.Where(doc => System.Data.Objects.SqlClient.SqlFunctions.DataLength(doc.BINARYCONTENT) > 50000);
Assert.IsTrue(results3.ToList().Count == 9);

第一个assert成功了,为什么results2和results 3执行的时候会出现如下异常?

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: LINQ to Entities does not recognize the method 'System.Nullable`1[System.Int32] DataLength(Byte[])' method, and this method cannot be translated into a store expression.

有什么方法可以让其他断言成功吗?

最佳答案

事实证明,答案是我使用了错误的函数。

代替

System.Data.Objects.SqlClient.SqlFunctions.DataLength

我应该用

System.Data.Entity.SqlServer.SqlFunctions.DataLength

位于 EntityFramework.SqlServer.dll 中

关于c# - 错误 : LINQ to Entities does not recognize the method DataLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com