The NotMapped attribute can be applied to properties of an entity class for which we do not want to create corresponding columns in the database. By default, EF creates a column for each property (must have get; & set;) in an entity class. The [NotMapped]
attribute overrides this default convention. You can apply the [NotMapped]
attribute on one or more properties for which you do NOT want to create a corresponding column in a database table.
NotMapped Attribute:
[NotMapped()]
using System.ComponentModel.DataAnnotations.Schema;
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
[NotMapped]
public int Age { get; set; }
}
In the above example, the
[NotMapped]
attribute is applied to the Age
property of the Student
class. So, EF will not create a column to store Age information in the Student db table, as shown below.
Note: EF also does not create a column for a property which does not have either getters or setters. For example, EF will not create columns for the following
City
and Age
properties.using System.ComponentModel.DataAnnotations;
public class Student
{
private int _age = 0;
public int StudentId { get; set; }
public string StudentName { get; set; }
public string City { get{ return StudentName;} }
public int Age { set{ _age = value;} }
}
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.