Data Annotation Feature-NotMapped, Annotation Feature-notmapped
The NotMapped feature can be applied to the attributes of the domain class. The default Code-First Convention creates data columns for all attributes with get, and set attribute selectors ..
The NotManpped feature breaks this Convention. You can use the NotMapped feature to add a property. Then Code-First will not create columns for this property in the data table.
Let's take a look at the following code:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{ [Table("StudentMaster",Schema="WaHaHa")] public class Student { [Key] [Column(Order=5)] public int StudentKey1 { get; set; } [Key] [Column(Order=6)] public int StudentKey2 { get; set; } [MaxLength(20)] [ConcurrencyCheck] [Required] [Column("SName",Order=1,TypeName="nvarchar")] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StandardRefId { get; set; } [ForeignKey("StandardRefId")] public virtual Standard Standard { get; set; } }}
Note: No. The table does not contain the Age column.
However, if only the Get attribute accessors or the set attribute accessors are available, Ef Code-First will not create data columns for them.
See:
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF2{ [Table("StudentMaster",Schema="WaHaHa")] public class Student { [Key] [Column(Order=5)] public int StudentKey1 { get; set; } [Key] [Column(Order=6)] public int StudentKey2 { get; set; } [MaxLength(20)] [ConcurrencyCheck] [Required] [Column("SName",Order=1,TypeName="nvarchar")] public string StudentName { get; set; } [NotMapped()] public int? Age { get; set; } public int StandardRefId { get; set; } public string FirstName { get { return FirstName; } } public int myAge; public int MyAge { set { value = myAge; } } [ForeignKey("StandardRefId")] public virtual Standard Standard { get; set; } }}
The obtained database is still like this: