Entity Framework Core 3.1 - Oracle“指定的强制转换无效" [英] Entity Framework Core 3.1 - Oracle “Specified cast is not valid”
问题描述
在我的 .NET Core 3.1 应用程序中,我使用 linq 使用以下语句从视图中获取条目列表:
In my .NET Core 3.1 application I am using linq to get a list of entries from a view using the following statement:
List<VEntries> entryList = dbContext.VEntries.Where(x => x.Timestamp.HasValue &&
x.Timestamp.Value.Date == dateTimeLocal.Date &&
x.Timestamp.Value.Hour == dateTimeLocal.Hour)
.ToList();
dateTimeLocal
在这种情况下等于在本地时间搜索条目的时间戳.
dateTimeLocal
in this case equals the timestamp to search entries for in local time.
此代码在我的本地环境中运行良好,但在生产系统上会引发以下异常:
This code works fine in my local environment but on the production system it throws the following exception:
System.InvalidCastException:指定的转换无效.
System.InvalidCastException: Specified cast is not valid.
在 Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
在 lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
在 Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() 在 System.Collections.Generic.List
1..ctor(IEnumerable1 collection) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable
1个源)
at Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)
数据库视图在两个系统上是相同的,并且包含一个 DATE
类型的列 Timestamp
,它可以为空:
The database view is identical on both systems and contains a column Timestamp
of type DATE
which is nullable:
生成的模型如下所示:
public partial class VEntries
{
public string QualityData { get; set; }
public decimal? Value { get; set; }
public DateTime? Timestamp { get; set; }
public decimal? Average { get; set; }
public decimal? Min { get; set; }
public decimal? Max { get; set; }
}
这里可能有什么问题?
推荐答案
根据你贴出的错误,我认为错误与DateTime
字段无关,而是与decimal有关
代替:
According to the error you posted, I believe the error is not related to the DateTime
field, but with the decimal
ones instead:
在 Oracle.ManagedDataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
Oracle 的 NUMBER
范围大于 C# decimal
,因此它可能无法将生产数据库中读取的一个非常大的数字转换为您的应用程序.我敢打赌,本地是有效的,因为在您的本地数据库中,您没有那么大的数字.
Oracle's NUMBER
range is bigger than C# decimal
, so it is probably not managing to convert one very large number read in the production database into your application. I bet that locally is working because in your local database you don't have any number as big.
可能的解决方案:
- 在您的生产数据库中截断或舍入小数(如果它是一个选项).
- 如果第一个选择是不可能的,您可以尝试在
VEntries
中为您的十进制值添加一个ValueConverter
,它从double
转换而来(精度较低但范围较大)到decimal
(精度较高,范围较小),反之亦然.
- Truncate or round decimals in your production database (if it is an option).
- If the first alternative isn't possible, you could try adding a
ValueConverter
for your decimal values inVEntries
that converts from adouble
(less precision but bigger range) todecimal
(more precision, less range) and vice-versa.
我还没有尝试过,但应该看起来像这样:
I haven't tried it, but should look something like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<VEntries>()
.Property(e => e. Value)
.HasConversion(
decimalValue => (double)decimalValue,
doubleValue => (decimal)doubleValue);
// Repeat for all decimal values
}
这篇关于Entity Framework Core 3.1 - Oracle“指定的强制转换无效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!