码迷,mamicode.com
首页 > 其他好文 > 详细

EF Code-First 学习之旅 DataAnnotations

时间:2017-03-28 23:55:40      阅读:519      评论:0      收藏:0      [点我收藏+]

标签:属性   create   return   current   span   rem   efi   多个   script   

数据注解:配置选项的子集;Fluent API包含所有选项

System.ComponentModel.DataAnnotations Attributes:

AttributeDescription
Key 标记实体的属性映射到数据库表中的主键
Timestamp 标记助兴为不可空的时间戳列(行版本)
ConcurrencyCheck 标记一个或多个属性做并发检查(当用户编辑或删除数据的时候) 
Required 属性必须有值
MinLength 设置属性类型为数组或字符串的最小长度
MaxLength MaxLength annotation is the maximum length of property which in turn sets the maximum length of a column in the database
StringLength Specifies the minimum and maximum length of characters that are allowed in a data field.

 

System.ComponentModel.DataAnnotations.Schema Attributes:

AttributeDescription
Table Specify name of the DB table which will be mapped with the class
Column Specify column name and datatype which will be mapped with the property
Index Create an Index for specified column. (EF 6.1 onwards only)
ForeignKey Specify Foreign key property for Navigation property
NotMapped Specify that property will not be mapped with database
DatabaseGenerated DatabaseGenerated attribute specifies that property will be mapped to computed column of the database table. So, the property will be read-only property. It can also be used to map the property to identity column (auto incremental column).
InverseProperty InverseProperty is useful when you have multiple relationships between two classes.
ComplexType Mark the class as complex type in EF.

 

Key

Code First默认以ID或{类名}+Id作为主键

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }

    [Key]
    public int StudentKey { get; set; }
     
    public string StudentName { get; set; }
        
}

技术分享

 

 可以创建混合主键,

 

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }
    [Key]
    [Column(Order=1)]
    public int StudentKey1 { get; set; }
     
    [Key]
    [Column(Order=2)]
    public int StudentKey2 { get; set; }
     
    public string StudentName { get; set; }
        
}

  

 技术分享

 

注:int型主键默认为自增列;混合型的主键不会设置为自增列;

 

 

TimeStamp

作用在字节数组上,创建数据类型为timestamp 的列,Code First自动用该列来检查并发

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }

    public int StudentKey { get; set; }
     
    public string StudentName { get; set; }
        
    [TimeStamp]
    public byte[] RowVersion { get; set; }
}
     

 

 

 

ConcurrencyCheck Attribute:

ConcurrencyCheck作用在实体的属性上,当进行更新操作时,在where子句中会带上ConcurrencyCheck作用的列上,作为查询条件

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }

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

 

exec sp_executesql NUPDATE [dbo].[Students]
SET [StudentName] = @0
WHERE (([StudentId] = @1) AND ([StudentName] = @2))
,N@0 nvarchar(max) ,@1 int,@2 nvarchar(max) ,@0=NSteve,@1=1,@2=NBill
go            
    

 

 TimeStamp作用在字节数组上,ConcurrencyCheck 作用在任何数据类型上

 

Required Attribute

using System.ComponentModel.DataAnnotations;
    
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [Required]
    public string StudentName { get; set; }
        
}

技术分享

 

 在表中的表现为对应的列是不可为null

MaxLength Attribute:

作用于实体的字符串和数组上。

对应表中列的字段数据类型为nvarchar

using System.ComponentModel.DataAnnotations;
    
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [MaxLength(50)]
    public string StudentName { get; set; }
        
}
    

技术分享

varchar如下表示:

[Column(TypeName="varchar")]

 

MinLength:

是一个验证属性,它与数据库没对应,ef会抛出异常

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [MaxLength(50),MinLength(2)]
    public string StudentName { get; set; }
        
}

 

StringLength Attribute

using System.ComponentModel.DataAnnotations;

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [StringLength(50)]
    public string StudentName { get; set; }
        
}
  

 

 

 技术分享

ef会自己验证属性

 

Table Attribute

using System.ComponentModel.DataAnnotations.Schema;

[Table("StudentMaster")]
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    public string StudentName { get; set; }
        
}

 

 

 技术分享

 

 

using System.ComponentModel.DataAnnotations.Schema;

[Table("StudentMaster", Schema="Admin")]
public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    public string StudentName { get; set; }
        
}

 

 

 技术分享

 

 

Column Attribute

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [Column("Name")]
    public string StudentName { get; set; }
        
}

 

技术分享

 

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
     
    [Column("Name", Order=1, TypeName="varchar")]
    public string StudentName { get; set; }
        
}

技术分享

 

 

ForeignKey Attribute

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
   
    }

 

 技术分享

 

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardRefId { get; set; }

    [ForeignKey("StandardRefId")]
    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
   
}

技术分享

 

 

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    
    [ForeignKey("Standard")]
    public int StandardRefId { get; set; }

    public Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
   
}

NotMapped Attribute

public class Student
{
    public Student()
    { 
        
    }

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

技术分享

 

 

 

 

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

 

如果属性不包括setter或getter,则不映射到表中

InverseProperty Attribute

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public Standard CurrentStandard { get; set; }
    public Standard PreviousStandard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    [InverseProperty("CurrentStandard")]
    public ICollection<Student> CurrentStudents { get; set; }
        
    [InverseProperty("PreviousStandard")]
        public ICollection<Student> PreviousStudents { get; set; }
   
    }

技术分享

 

 

public class Student
{
    public Student()
    { 
        
    }
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int CurrentStandardId { get; set; }
    public int PreviousStandardId { get; set; }

    [ForeignKey("CurrentStandardId")]
    public Standard CurrentStandard { get; set; }
        
    [ForeignKey("PreviousStandardId")]
    public Standard PreviousStandard { get; set; }
}

public class Standard
{
    public Standard()
    { 
        
    }
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    [InverseProperty("CurrentStandard")]
    public ICollection<Student> CurrentStudents { get; set; }
        
    [InverseProperty("PreviousStandard")]
    public ICollection<Student> PreviousStudents { get; set; }
   
}

技术分享

 

EF Code-First 学习之旅 DataAnnotations

标签:属性   create   return   current   span   rem   efi   多个   script   

原文地址:http://www.cnblogs.com/lanpingwang/p/6637482.html

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