question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
chsandeepkumarcommented, May 15, 2019

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.

0reactions
vicentesuccommented, Apr 2, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting 'Column is null' issues using Postgres and ...
Run a SQL trace while the code is running and see what the last SQL that run was. That is likely the problematic...
Read more >
Class NpgsqlDataReader
Indicates whether the reader is currently positioned on a row, i.e. whether reading a column is possible. This property is different from HasRows...
Read more >
ASP.NET - Retrieving data from SQL gives an error
I'm trying to run my ASP.NET project but get a error - "System.InvalidCastException:" I don't understand why, I checked all the types and ......
Read more >
SqlDataReader.GetFieldValue<T>(Int32) Method
The value of the column was null (IsDBNull(Int32) == true ), retrieving a non-SQL type. InvalidCastException. T doesn't match the type returned by...
Read more >
Untitled
InvalidCastException : Column is null Stack Trace `at Npgsql.NpgsqlDefaultDataReader.GetFieldValue[T](Int32 column) in … https://github.com/npgsql/efcore.pg/ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found