实体框架 +优势数据库:转换可为空的数字类型

发布于 2024-09-15 12:00:43 字数 654 浏览 4 评论 0原文

我在我的应用程序中使用 VS2010、Entity Framework 4.0 和 Advantage v.10。我编写了一个 Linq-to-Entities (L2E) 语句,尝试将可为空的数字(小数)类型转换为小数。一个简单的语句可能如下所示:

var x = (from test in entities.Tests
         select test.ValueA.HasValue ? test.ValueA.Value : 0);

但是,我收到以下错误:

System.Data.EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。 ---> Advantage.Data.Provider.AdsException:错误 7200:AQE 错误:状态 = S0000;本机错误 = 2159; [iAnywhere Solutions][Adv​​antage SQL Engine]标量函数的参数无效:CAST - 必须同时指定精度和小数位数。 -- SQL 语句中的错误位置为:xxx(行:x 列:x) AdsCommand 查询执行失败。

有没有办法解决这个枚举结果并在客户端进行转换的问题?我不知道如何通过L2E语句告诉Advantage“0”的精度和小数位数。

提前致谢。

I am using VS2010, Entity Framework 4.0, and Advantage v. 10 in my application. I have written a Linq-to-Entities (L2E) statement that tries to convert a nullable numeric (decimal) type to a decimal. A simple statement might look like:

var x = (from test in entities.Tests
         select test.ValueA.HasValue ? test.ValueA.Value : 0);

However, I am receiving the following error:

System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> Advantage.Data.Provider.AdsException: Error 7200: AQE Error: State = S0000; NativeError = 2159; [iAnywhere Solutions][Advantage SQL Engine]Invalid argument to scalar function: CAST - must specify both precision and scale. -- Location of error in the SQL statement is: xxx (line: x column: x) AdsCommand query execution failed.

Is there any way around this short of enumerating the results and doing the conversion on the client side? I am not sure how to tell Advantage the precision and scale of "0" through the L2E statement.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-09-22 12:00:43

正如 Craig 提到的,这是 Advantage Entity Framework Provider 中的一个错误。它将在 Advantage Entity Framework Provider 的下一个服务版本中修复。

另一种可能的解决方法是公开数据库的 IsNULL 函数。将以下内容添加到 SSDL。

 <Function Name="IsNull" ReturnType="numeric" Aggregate="false" BuiltIn="true" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion">
    <Parameter Name="Value" Type="numeric" Mode="In"/>
    <Parameter Name="Default" Type="integer" Mode="In"/>
 </Function>

然后添加以下 CLR 存根函数。

  public static class MyFunctions
     {

     [EdmFunction( "Model.Store", "IsNull" )]
     public static decimal IsNull( decimal? Value, int? Default )
        {
        throw new InvalidOperationException( "Call from within an L2E query" );
        }
     }

As Craig mentioned this is a bug in the Advantage Entity Framework Provider. It will be fixed in the next service release of the Advantage Entity Framework Provider.

Another possible work around would be to expose the IsNULL function of the database. Add the following to the SSDL.

 <Function Name="IsNull" ReturnType="numeric" Aggregate="false" BuiltIn="true" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion">
    <Parameter Name="Value" Type="numeric" Mode="In"/>
    <Parameter Name="Default" Type="integer" Mode="In"/>
 </Function>

Then add the following CLR stub function.

  public static class MyFunctions
     {

     [EdmFunction( "Model.Store", "IsNull" )]
     public static decimal IsNull( decimal? Value, int? Default )
        {
        throw new InvalidOperationException( "Call from within an L2E query" );
        }
     }
深海里的那抹蓝 2024-09-22 12:00:43

您的 EF 提供程序生成了错误的 SQL,这是提供程序中的错误。

但是,您也许可以解决它:

var x = entities.Tests
                .Select(t => t.ValueA)
                .AsEnumerable()
                .Select(t => t.GetValueOrDefault());

Your EF provider generated bad SQL, and that's a bug in the provider.

However, you can probably work around it:

var x = entities.Tests
                .Select(t => t.ValueA)
                .AsEnumerable()
                .Select(t => t.GetValueOrDefault());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文