Preventing "'System.DateTime' failed because the materialized value is null"

c# entity-framework entity-framework-6 entity-framework-6.1

Accepted Answer

Instead of choosing that option,Nullable , you may check to see if this is true by converting your query's value to a nullable type.null .

DateCreated = (DateTime?)subJoined.dateUploaded ?? DateTime.Now

Your inquiry will appear as follows:

using (var db = new ABEntities())
    var features = (from textObject in db.textObjects
                    join container in db.containers.DefaultIfEmpty() on textObject.textObjectPK equals container.textObjectPK into tObjsContainerJoined
                    from subContainerJoin in tObjsContainerJoined.DefaultIfEmpty()
                    join tObjsMedia in on subContainerJoin.mediaID equals tObjsMedia.mediaID into tObjsMediaJoined
                    from subJoined in tObjsMediaJoined.DefaultIfEmpty()
                    from textContainer in tObjsContainerJoined
                    where textObject.version == Constants.Versions.LATEST &&
                                    textObject.textObjectTypeID == Constants.News.FEATURES && textObject.deployDate <= DateTime.Now
                    select new TextObject
                          Id = textObject.textObjectID,
                          Title = textObject.title,
                          ContainerId = textContainer.containerID,
                          Description = textContainer.container1,
                          DateCreated = textObject.deployDate,
                          Media = new Media
                                       Title = subJoined.title,
                                       MediaFormat = subJoined.extension,
                                       MediaTypeID = subJoined.mediaTypeID,
                                       MediaFile = subJoined.fileName,
                                       Credit =,
                                       MembersOnly = subJoined.membersOnly,
                                       LastModified = subJoined.lastModified,
                                       DateCreated = (DateTime?)subJoined.dateUploaded ?? DateTime.Now
                           TypeId = textObject.textObjectTypeID
                   }).OrderByDescending(t => t.DateCreated).ToList();

   return features;

Why does that particular line include an error?

This is only a hunch, so please correct me if I'm wrong.

Most likely as a result of your left join, for which EF throws an error out of caution since it anticipates some values, can, to be null. The only error it issues is to theDateTime since it understands what to do when the other properties are null, datatype (strings and nullable types).

3/24/2015 9:46:53 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow