You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Null UUID field throws error "Column is NULL" when using EntityFramework #1023
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.
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:
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.
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 :-)).
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.
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...
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.
Activity
Emill commentedon Apr 11, 2016
Can you show a stack trace and the code you use to get this exception?
Thx1137 commentedon Apr 11, 2016
Sure.
Emill commentedon Apr 11, 2016
and your linq query as well?
Thx1137 commentedon Apr 11, 2016
A bit of the Client class:
If I change 'Guid' to 'string' there is no problem.
Regards,
Steven.
Emill commentedon Apr 11, 2016
I guess the Name column is a Guid as well then? Are the column types in the db of the "uuid" type?
Thx1137 commentedon Apr 11, 2016
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 :-)).
Emill commentedon Apr 12, 2016
I completely forgot Guid is a struct and not a class. You'll have to declare your nullable fields as "Guid?".
Thx1137 commentedon Apr 12, 2016
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.Thx1137 commentedon Apr 12, 2016
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...
Emill commentedon Apr 12, 2016
Try
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.
roji commentedon May 8, 2016
@Thx1137 have you tried our @Emill's suggestion? Does it solve your issue?
Thx1137 commentedon May 8, 2016
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.
roji commentedon May 9, 2016
Great thanks.