entity framework + advantage database: converting nullable numeric types
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 nul开发者_JS百科lable 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.
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" );
}
}
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());
精彩评论