zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(9.10):DataAnnotations

    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.

    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;}  }
        
    }
  • 相关阅读:
    IOI 1996 网络协议
    lougu P2344奶牛抗议
    Poj3764 The XOR-longest Path
    A Simple Problem with Integers (线段树)
    NOIP2011 选择客栈
    20181029 T3 乐谱分段
    20181029 T2 寻宝游戏
    20181029 T1 教科书般的亵渎
    NOIP2011聪明的质监员
    浅谈AC自动机
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644209.html
Copyright © 2011-2022 走看看