I am working with asp.net mvc 4 and entity framework code-first approach. I have the following entity model
public class User
{
public virtual int Id { get; set; }
public virtual String FirstName { get; set; }
public virtual String MiddleName { get; set; }
public virtual String LastName { get; set; }
}
For the user list view page I need to show Full Name, I am trying to create a get only property in user model like
public virtual String FullName{
get{
return FirstName + " " + MiddleName + " " + LastName;
}
}
But I am not sure if it will work, besides I also don’t want entity framework to map this to a database column.
Can I use similar get only property generated from other properties for only view?
Can any one can give me any suggestion on what to do??
Depending on how you are configuring your Domain Entities, via DataAnnotations or EntityConfigurationType classes, you could either use the [NotMapped] DataAnnotation
or in a specific class that implements EntityTypeConfiguration, you can set the property to be ignored by EntityFramework like so and then add it to your contexts configurations collection in the OnModelCreating event: