efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

System.InvalidCastException: Column is null at Npgsql.NpgsqlDefaultDataReader.GetFieldValue[T](Int32 column)

Open chsandeepkumar opened this issue 7 years ago • 7 comments

could you please someone help me on this? did i miss anything in my model , same code works fine when i was using MSSql Server and EFCore , but failed when we switch database to postgresql and npgsql

software versions i have used npgsql : Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0 efcore : Microsoft.EntityFrameworkCore 2.2.0

my Entity

        public Guid Id { get; set; }
        public long OrderNumber { get; set; }
        public Guid CustomerId { get; set; }
        public Guid? XgentId{ get; set; }
        public Guid? AdminUserId { get; set; }
        public Guid? AgentUserId { get; set; }
        public int Year { get; set; }
        public int? ShippingMethodId { get; set; }       
        public Guid? OrderReferenceId { get; set; }
        public long? BatchId { get; set; }
        public DateTime? PostedDate { get; set; }       
        public long? DailySalesId { get; set; }
        public Guid? RelatedOrderId { get; set; }       
        public int? OverpaymentReasonId { get; set; }
        public decimal? CustomPayment { get; set; }       
        public decimal? Fee { get; set; }
        public DateTime? FeeCollected { get; set; }
        public long? SweepId { get; set; }
        public string InvoiceTransId { get; set; }
        public int? ReprintCount { get; set; }
        public DateTime? Modified { get; set; 
}

my linq query

var results= _context.Order.ToArray();

stack trace:

 An exception occurred while iterating over the results of a query for context type 'myDBContext'.    System.InvalidCastException: Column is null
         
Stack Trace `at Npgsql.NpgsqlDefaultDataReader.GetFieldValue[T](Int32 column) in C:\projects\npgsql\src\Npgsql\NpgsqlDefaultDataReader.cs:line 92
   at lambda_method(Closure , DbDataReader )
   at Microsoft.EntityFrameworkCore.Storage.Internal.TypedRelationalValueBufferFactory.Create(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) in C:\projects\npgsql-entityframeworkcore-postgresql\src\EFCore.PG\Storage\Internal\NpgsqlExecutionStrategy.cs:line 28
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at CDNR.Jobs.AchSweep.DataAccess.GetOrdersForSweep1() in C:\Work\SourceCode\VSTS\CDNRJobs\CDNR.Jobs.AchSweep\DataAccess.cs:line 39
   at CDNR.Jobs.AchSweep.Job.ExecuteAsync() in C:\Work\SourceCode\VSTS\CDNRJobs\CDNR.Jobs.AchSweep\Job.cs:line 42

chsandeepkumar avatar May 14 '19 04:05 chsandeepkumar

@chsandeepkumar any chance you could narrow it down by omitting properties from that class until you find which one is triggering the problem?

roji avatar May 15 '19 01:05 roji

Thanks @roji , I have done that just now , and identified that public Guid CustomerId { get; set; } is modified in a database nullable to true, when i generated my model back again using database first approach, my model is like this

Guid? CustomerId { get; set; }

my table contains also null values for CustomerId, this makes sense to me now, my models is not up to date with database schema. @roji

I think i can close this issue and sorry for it.

chsandeepkumar avatar May 15 '19 16:05 chsandeepkumar

I got here searching for an explanation as to why the error does not indicate which column is null. Would be a nice feature, non? I will divide and conquer until I've identified the column, but it seems a colossal waste of time when the error was thrown by something that knew which column is the problem.

Am thankful for this library, please consider this a friendly request for an added feature.

WellspringCS avatar Nov 22 '19 13:11 WellspringCS

@WellspringCS FWIW this is the way almost all .NET APIs work - KeyNotFoundExceptions generated by dictionary lookups also don't provide the key name. IIRC there's a small perf argument behind it being this way, but I'm not sure it's very valid here. Either way if you think this is valuable please open a new issue for it.

roji avatar Nov 22 '19 22:11 roji

Ahhh, I was unaware. It'd be an awesome improvement, but I get it if it's not as easy as it seemed to implement. Thx.

WellspringCS avatar Nov 22 '19 23:11 WellspringCS

Already opened it, @roji.

YohDeadfall avatar Nov 23 '19 05:11 YohDeadfall

if somebody has the same problem, i fixed as @chsandeepkumar said, .. check your database , the primary key column , remove NOT NULL and try again.. it worked for me .

Please check all the table , that you don't have NULL columns beacuse, EF cant not serialize that empty colums

regards.

vicentesuc avatar Apr 01 '20 23:04 vicentesuc