码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-07-05 17:01:56      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

DataAnnotations - NotMapped Attribute:

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.

技术分享

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;}  }
    
}

 

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

标签:

原文地址:http://www.cnblogs.com/purplefox2008/p/5644209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!