Nullable entity projection in Entity Framework?

You forgot to include the declaration of your "Message" class but I suspect that the UserID property in that class is not declared as a nullable type. If that is the case, change it from "int" to "int? " (nullable int).

1 I think that this discussion on the MS forums also applies. It probably does the same thing as what this answer suggests. Social.msdn.microsoft.Com/forums/en-US/adodotnetentityframework/… – villecoder Jun 25 '10 at 14:29.

Right, this is 1:n relationship, a message belongs to only 1 user, but a user can have several messages. – liggett78 Sep 26 '08 at 15:57 What does the m. Users property do?

I assumed that was a bunch of User associated with the Message. – leppie Sep 26 '08 at 16:10 OK, m. Users should actually be m.

User, I've changed it in the original question. This propery is an entity pointing to the user that submitted this message. – liggett78 Sep 26 '08 at 16:56.

Since you did an . Include("Users") you should be able to just traverse the User property in the Message object to get the information your want.

Yes, this is the way I do it, except when there is no user associated with the message (remember, UserID is nullable). – liggett78 Sep 26 '08 at 16:57 If there's no user object, then you'd want a null value. What are you looking for instead?

– Orion Adrian Sep 26 '08 at 20:45.

Now note that I advise the entity framework - using Include("Users") - to load a user associated with a message, if any. Also note that I don't call ToList() after the first LINQ statement. By doing so only specified columns in the projection list - in this case MessageID, UserID, FirstName - will be returned from the database.

Here lies the problem - as soon as Entity Framework encounters a message with UserID == NULL, it throws an exception, saying that it could not convert to Int32 because the DB value is NULL. Then a run-time NotSupportedException is thrown telling that it can't create a constant User type and only primitives like int, string, guid are supported. Anybody has any idea how to handle it besides materializing the results just right after the first statement and using in-memory projection afterwards?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions