I have the following problem: I have a table in the database called Person which have all the relevant data such as first name, last name, date of birth, sex etc ... . My question is : Is it possible to hide some of the attributes and if yes how can i achieve that. I need that because in my entity instead of date of birth I want an attribute called age which will take the date of birth and calculate the age. Also I want to hide another column called job which has default value N for no and also can be Y for yes. Instead of it I want to have the same column but with true or false. I know that I can achieve that changing the database but in my case I am not allowed to do that. And the last point: is there away to add additional columns which doesn't have a representation in the database ..for example a computed one which takes the attribute salary and based on it (for example if it is more or less than 500 euros) calculates the bonuses ? Thanks :)
1 Answer
Place your context and entities into a seperate project. The Person entity you've described could be done as follows:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
internal DateTime DateOfBirth { get; set; }
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public double AgeInYears { get { return DateTime.Now.Subtract(this.DateOfBirth).TotalDays / 365; } }
public char Sex { get; set; }
internal string Job { get; set; }
[System.ComponentModel.DataAnnotations.Schema.NotMapped]
public bool HasJob { get { return this.Job == "Y"; } }
}
Doing the above will only expose FirstName, LastName, AgeInYears, Sex, and HasJob to other projects, in the datatype you want.
To add a column that doesn't exist in the database, just use the appropriate Data Annotation as shown above.
To hide a column, mark it as internal.
Hope that helps.