I have defined an Enum property on an Entity via Fluent API as IsOptional. The database reflects the IsOptional as it shows it as a nullable type. When I attempt to query this Entity property for null values I get the following error:
The 'UserType' property on 'Group' could not be set to a 'null' value.
You must set this property to a non-null value of type 'UserType'.
The query is as follows:
var groups = (from g in db.Groups
let reqs = from r in db.Requests
where r.Id == requestId from gg in r.Groups select gg.Id
where g.ContentArea.Id == db.Requests.FirstOrDefault(o => o.Id == requestId).ContentArea.Id
where !reqs.Contains(g.Id)
where (g.UserType == db.Requests.FirstOrDefault(r => r.Id == requestId).User.UserType || g.UserType == (UserType?)null)
select g).ToList();
and the part that specifically breaks is after the OR statement
g.UserType == (UserType?)null
I've tried to compare g.UserType to null, set an instance of UserType? nullType = null and compared that but nothing seems to work. It seems to be a shortcoming of EF. Any suggestions?
EDIT: Included the entire query as requested.
The problem is not in the structure of the statement, it's in the entities EF tries to materialize.
Group
has a propertyUserType
that's not nullable. But in the database it is nullable and some of the returned records have null values for it.So you either have to make the property nullable:
or make sure that the statement doesn't return null values.