In one of my projects I am working on I am using Entity Framework 4.1 (Code First). I have a relationship between two entities like the following:
public class Project
{
public int Id { get; set; }
// snip...
// Foreign Key
public string ProjectId { get; set; }
// navigation proeprty
public virtual ProjectType ProjectType { get; set; }
}
public class ProjectType
{
public string Id { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
Right now I business logic that depends on what type of project is being created/edited so I have code like this:
if( "P".Equals(project.ProjectTypeId) )
// logic goes here
Is there some other way to do this that doesn't rely on me comparing string values?
I'd personally prefer converting ProjectTypeId to an enum type.
I'm assuming that you have a fixed number of ProjectTypes, and that your code is supposed to be aware of all of them. This approach gives you a single "source of truth" to look at when you need to see all the ProjectTypes that can be used. I prefer this over other options like a class with string constants because:
ProjectType
s around as strongly-typed parameters to utility functions and such.