Skip to content

Null UUID field throws error "Column is NULL" when using EntityFramework #1023

Closed

Description

@Thx1137

I have a postgres table that has several using UUID columns. These are accessing in Entityframework using the GUID data type. I am using Postgres 9.5 and NPGSQL 3.0.5.

In the database we have two rows:

Row 1 has
ID = 70361cbd-a1cf-4565-8111-2d2175844ce6
Name = 3c7c0ba0-cdf0-11e5-84c6-9f2eba908084

Row 2 has
ID = 3c82756c-cdf0-11e5-84d3-eb7965fcfd58
Name = NULL

If I select row 1 using ID=70361cbd-a1cf-4565-8111-2d2175844ce6 it works.
If I select row 2 using ID=3c82756c-cdf0-11e5-84d3-eb7965fcfd58 the "Column is Null" exception is thrown.

Activity

  1. Emill commented on Apr 11, 2016

    @Emill
    Contributor

    Can you show a stack trace and the code you use to get this exception?

  2. Thx1137 commented on Apr 11, 2016

    @Thx1137
    Author

    Sure.

    System.InvalidCastException occurred
      HResult=-2147467262
      Message=Column is null
      Source=Npgsql
      StackTrace:
           at Npgsql.BackendMessages.DataRowMessage.CheckNotNull()
           at Npgsql.NpgsqlDataReader.ReadColumnWithoutCache[T](Int32 ordinal)
           at Npgsql.NpgsqlDataReader.ReadColumn[T](Int32 ordinal)
           at Npgsql.NpgsqlDataReader.GetGuid(Int32 ordinal)
           at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.ReadGuid(DbDataReader reader, Int32 ordinal)
           at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(DbDataReader reader, DbSpatialDataReader spatialDataReader, Type[] columnTypes, Boolean[] nullableColumns)
           at System.Data.Entity.Core.Objects.Internal.ShapedBufferedDataRecord.Initialize(String providerManifestToken, DbProviderServices providerServices, DbDataReader reader, Type[] columnTypes, Boolean[] nullableColumns)
           at System.Data.Entity.Core.Objects.Internal.BufferedDataReader.Initialize(String providerManifestToken, DbProviderServices providerServices, Type[] columnTypes, Boolean[] nullableColumns)
           at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
           at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a()
           at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
           at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9()
           at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
           at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
           at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
           at System.Lazy`1.CreateValue()
           at System.Lazy`1.LazyInitValue()
           at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
           at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
           at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__2[TResult](IEnumerable`1 sequence)
           at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
           at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
           at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
           at App_JmcPortal.Models.Client.<>c__DisplayClass2.<GetById>b__1(JmcPortalDataContext dc) in c:\Programming\Source\App_JmcPortal\App_JmcPortal\Code\Models\Client.cs:line 44
      InnerException: 
    
  3. Emill commented on Apr 11, 2016

    @Emill
    Contributor

    and your linq query as well?

  4. Thx1137 commented on Apr 11, 2016

    @Thx1137
    Author
    public static Client GetById(Guid clientId)
    {
        using (var dataContext = new JmcPortalDataContext())
        {
            return (from c in dataContext.Clients
                    where c.Id == clientId
                    select c).FirstOrDefault();
        }
    }
    

    A bit of the Client class:

            /// <summary>
            /// The database primary key.
            /// </summary>
            [Key, Column("id")]
            public Guid Id { get; set; }
    
            /// <summary>
            /// The client contact id.
            /// </summary>
            [Column("contact_id")]
            [Display(Name = "Contact")]
            public Guid ContactId { get; set; }
    
    

    If I change 'Guid' to 'string' there is no problem.

    Regards,
    Steven.

  5. Emill commented on Apr 11, 2016

    @Emill
    Contributor

    I guess the Name column is a Guid as well then? Are the column types in the db of the "uuid" type?

  6. Thx1137 commented on Apr 11, 2016

    @Thx1137
    Author

    Sorry, Name was an example as I was running out the door, substitute Name with ContactId. I'll log it better next time :-)

    Yes, both id and contact_id are postgres UUID columns. id is a NOT NULL PK and contact_id is a NULLABLE UUID field. I took the FK constraint off during testing which made no difference (surprise :-)).

  7. Emill commented on Apr 12, 2016

    @Emill
    Contributor

    I completely forgot Guid is a struct and not a class. You'll have to declare your nullable fields as "Guid?".

  8. Thx1137 commented on Apr 12, 2016

    @Thx1137
    Author

    I'm not sure what that means, it seems like a question and statement in one :-)

    Are you saying it can't work because it isn't nullable? In a WHERE clause I used (clientIdFilter == Guid.Empty || c.ClientID == clientIdFilter) (a conditional filter) and that worked so I thought it would be ok. IE: That assigning Guid.Empty would be translated to NULL on the Db side as it seems to do in the where clause.

  9. Thx1137 commented on Apr 12, 2016

    @Thx1137
    Author

    It isn't necessary for this to work because just using a string containing the guid works but type safety is a nice thing :-) It is almost there it is just nullable FKs...

  10. Emill commented on Apr 12, 2016

    @Emill
    Contributor

    Try

    public Guid? ContactId { get; set; }
    

    Guid.Empty doesn't mean null, it's rather a Guid with all bytes set to zero.
    Just like string.Empty is not null but a string with length 0.

  11. roji commented on May 8, 2016

    @roji
    Member

    @Thx1137 have you tried our @Emill's suggestion? Does it solve your issue?

  12. Thx1137 commented on May 8, 2016

    @Thx1137
    Author

    G'day, I had already swapped the guids with ints so haven't had a chance to try it. Emil's suggestion seems obvious when thought about. I'm disappointed I hadn't done that first. I suspect it will work so I will close this and reopen if it still occurs when I can try it again.

  13. roji commented on May 9, 2016

    @roji
    Member

    Great thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions