NotMapped attribute can be applied to properties of a class. Default Code-First convention creates a column for all the properties which includes getters and setters. NotMapped attribute overrides this default convention. You can apply NotMapped attribute to a property which you do NOT want to create a column in a database table for.

Consider the following example.

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }

    public int StudentId { get; set; }
     
    public string StudentName { get; set; }
        
    [NotMapped]
    public int Age { get; set; }
}

 

As you can see in the above example, NotMapped attribute is applied to Age property of the Student class. So, Code First will not create a column to store Age information in the Student table as shown below.

Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

Code-first also does not create a column for a property which does not have either getters or setters. Code-First will not create columns for FirstName and Age properties in the following example.

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }
    private int _age = 0;

    public int StudentId { get; set; }
     
    public string StudentName { get; set; }
    
    public string FirstName { get{ return StudentName;}  }
    public string Age { set{ _age = value;}  }
    
}

 

相关文章:

  • 2022-02-27
  • 2021-09-21
  • 2021-11-02
  • 2021-08-30
  • 2021-12-19
  • 2021-12-03
  • 2022-12-23
  • 2021-07-24
猜你喜欢
  • 2022-02-17
  • 2022-02-06
  • 2021-09-13
  • 2021-12-21
  • 2022-02-08
  • 2022-01-10
  • 2022-02-28
相关资源
相似解决方案