【译】第19节---数据注解-NotMapped

 原文:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx

NotMapped属性可以应用于类的属性。 默认的Code-First约定为包含getter和setter的所有属性创建一个列。 NotMapped属性覆盖此默认约定。 你可以将NotMapped属性应用于不希望在数据库表中创建列的属性。

请看以下示例:

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

如上例所示,NotMapped属性应用于Student类的Age属性。 所以,Code First不会创建一列来存储学生表中的Age信息,如下所示:

Code First还不会为没有getter或setter的属性创建一列。 在以下示例中,Code-First将不会为FirstName和Age属性创建列:

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;}  }
    
}
原文地址:https://www.cnblogs.com/talentzemin/p/7218402.html