In my new ASP.NET EF4 project, I find myself having to do a lot of maneuvering around null entities. For example, I may have a Project Entity with a related User Entity exposed through an Assignee property. If, for example, I need to send an email to someone indicating who the Assignee is, I need to say:
string x = "The Assignee is: " + TheProject.Assignee.Name;
but I'll get an exception if there is no Assignee as the property is null (I want nulls by design) and I can't fetch the .Name property on that null. So I have to do this:
string x = "The Assignee is: " + TheProject.Assignee == null ? "", TheProject.Assignee.Name
This is tedious and messy, at best, and I find myself having to do it all over the place in my code. Has anyone come up with an elegant solution for this?
I also realize I can build a helper method, or an extension method (loving these!) but I'd have to hard code for the .Name property, which isn't always what I want. I also want to avoid doing a whole bunch of reflection.