Repository navigation
Todo
Description
Version: EntityFrameworkCore.LibRed 11.0.0-beta.1
TargetFramework: net11.0-windows
Problem
In an Access XP mdb I have a query "Quarterly Orders by Product" with an Integer (16-bit) column yielded by the Year function(Year([OrderDate]) AS OrderYear):
TRANSFORM Sum(CCur([Order Details].[UnitPrice]*[Quantity]*(1-[Discount])/100)*100) AS ProductAmount
SELECT Products.ProductName, Orders.CustomerID, Year([OrderDate]) AS OrderYear
FROM Products INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID
WHERE (((Orders.OrderDate) Between #1/1/1995# And #12/31/1995#))
GROUP BY Products.ProductName, Orders.CustomerID, Year([OrderDate])
PIVOT "Qtr " & DatePart("q",[OrderDate],1,0) In ("Qtr 1","Qtr 2","Qtr 3","Qtr 4");It is mapped to this class:
public class QuarterlyOrdersByProduct
{
public string ProductName { get; set; } = null!;
public string? CustomerID { get; set; }
public short? OrderYear { get; set; }
public decimal? Qtr1 { get; set; }
public decimal? Qtr2 { get; set; }
public decimal? Qtr3 { get; set; }
public decimal? Qtr4 { get; set; }
public override string ToString() => $"{ProductName}, {CustomerID}, {OrderYear}, {Qtr1}";
}Configuration:
public DbSet<QuarterlyOrdersByProduct> QuarterlyOrdersByProducts { get; set; } = null!;optionsBuilder.UseLibRed(@"C:\Users\...\NWIND_xp.MDB");modelBuilder.Entity<QuarterlyOrdersByProduct>(mb => {
mb.ToView("Quarterly Orders by Product").HasNoKey();
mb.Property(e => e.Qtr1).HasColumnName("Qtr 1");
mb.Property(e => e.Qtr2).HasColumnName("Qtr 2");
mb.Property(e => e.Qtr3).HasColumnName("Qtr 3");
mb.Property(e => e.Qtr4).HasColumnName("Qtr 4");
});I get this exception when trying to query it with EF Core:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'System.Int32' to type 'System.Int16'.
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.CompilerServices.CastHelpers.ThrowInvalidCastExceptionInternal(Void* fromTypeHnd, Void* toTypeHnd)
at System.Runtime.CompilerServices.CastHelpers.ThrowInvalidCastException(Object fromType, Void* toTypeHnd)
at System.Runtime.CompilerServices.CastHelpers.Unbox_Helper(MethodTable* pMT1, Object obj)
at LibRed.Data.LibRedDataReader.GetInt16(Int32 ordinal)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at ... my own code
Access mdb: https://cysoft.ch/Sources/NWIND_xp_mdb.zip (yes, it is the good old Northwind database converted from Access 97 to XP)
Metadata
Metadata
Assignees
Labels
No labels
Activity
ChrisJollyAU commentedon Nov 25, 2026
Ha, the legendary Northwind. A version of that actually pins a large portion of the libred tests in fact.
In LibRed, I had Year, Month, Day etc mapped with a result int32 (comes from the c# equivalent of DateTime.Year being the int32). But Year etc do in fact with ACE return a short (int16). I have fixed that in my working branch so that will come through on the next release.
In the mean time the easy workaround is to in your class declare OrderYear as a normal c# int/Int32
OJacot-Descombes commentedon Nov 25, 2026
Thank you for the prompt reply.
In case you wonder why I am using Northwind, I am working on an application which converts Access applications to .NET WinForms applications. It reverse engineers the databases (local or linked) to create DbContexts including all the configuration (so far only Access and SQL Server) and creates the corresponding model classes. Then it converts the Access Forms to Window Forms including data bindings. Finally controller classes connect the database to the forms. I am retired, but well, you need some hobbies :-)