none
Error - {"The specified cast from a materialized 'System.Int64' type to a nullable 'System.Int32' type is not valid."}

    Question

  • Hi,

    I ran the codes below...
                using (Entities sp = new Entities())  
                {  
     
                    var query = sp.CreateQuery<ORDERS>("SELECT O.OR_NUMBER FROM Entities.ORDERS AS O");  
     
                    foreach (var record in query)  //error here
                    {  
                        Console.WriteLine(record.OR_NUMBER);  
                    }  
                    Console.ReadLine();  
                } 
    using the (ssdl) model below...
     <EntityType Name="ORDERS">  
              <Key> 
                <PropertyRef Name="OR_NUMBER" /> 
              </Key> 
              <Property Name="OR_CUSTOMER" Type="int" /> 
              <Property Name="OR_EDATE" Type="date" /> 
              <Property Name="OR_INVOICE" Type="int" /> 
              <Property Name="OR_ITEM" Type="int" /> 
              <Property Name="OR_NUMBER" Type="int" Nullable="false" /> 
              <Property Name="OR_ODATE" Type="date" /> 
              <Property Name="OR_PRICE" Type="decimal" Precision="7" Scale="2" /> 
              <Property Name="OR_QTY" Type="int" /> 
              <Property Name="OR_SDATE" Type="date" /> 
              <Property Name="OR_TERMS" Type="varchar" MaxLength="2" /> 
              <Property Name="OR_VENDOR" Type="int" /> 
            </EntityType> 

    and I get the error {"The specified cast from a materialized 'System.Int64' type to a nullable 'System.Int32' type is not valid."}.
       at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)  
       at System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal)  
       at lambda_method(ExecutionScope , Shaper )  
       at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)  
       at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()  
       at ConsoleApplication17.Program.Main(String[] args) in C:\Ayee\Codes\temp\ConsoleApplication17\ConsoleApplication17\Program.cs:line 17  
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)  
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)  
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()  
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)  
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)  
       at System.Threading.ThreadHelper.ThreadStart() 

    I checked the provider manifest and looks like the mapping were OK and I was able to generate the model with it. I can't find the reason why its materializing as an int64.

    Any help is appreciated, thanks!


    -Ayee
    Wednesday, February 04, 2009 10:07 AM

Answers

  • Is it possible the SSDL indicates 'int' as the type for a column that is actually a 'bigint'  the database? This would explain the exception you're seeing. The EF does not validate the contents of the SSDL with respect to the database at runtime so the first indication of a mismatch between what the SSDL claims and what the database contains is a problem materializing results.

    Thanks,
    -Colin
    This posting is provided "AS IS" with no warranties and confers no rights.
    • Marked as answer by ayee Wednesday, February 11, 2009 5:00 AM
    Monday, February 09, 2009 10:10 PM

All replies

  • There's something wrong with your esql - "select o.or_number from" selects the or_number field - you can't return it as an Orders entity type.

    either write "Select Value o from entities.orders as o" or change the generic type to DbDataRecord and extract the value from the returned record

    Ido.
    Wednesday, February 04, 2009 3:07 PM
  • Is it possible the SSDL indicates 'int' as the type for a column that is actually a 'bigint'  the database? This would explain the exception you're seeing. The EF does not validate the contents of the SSDL with respect to the database at runtime so the first indication of a mismatch between what the SSDL claims and what the database contains is a problem materializing results.

    Thanks,
    -Colin
    This posting is provided "AS IS" with no warranties and confers no rights.
    • Marked as answer by ayee Wednesday, February 11, 2009 5:00 AM
    Monday, February 09, 2009 10:10 PM
  • Thanks Colin! apparently you were right. The database I was using supports unsigned types and the provider should have reported the type as int64 in the EDM generation instead of int32.

    Thanks!
    Ayee
    Wednesday, February 11, 2009 5:13 AM