EF Code-First 学习之旅 DataAnnotations

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

System.ComponentModel.DataAnnotations Attributes:

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

 

System.ComponentModel.DataAnnotations.Schema Attributes:

AttributeDescription
TableSpecify name of the DB table which will be mapped with the class
ColumnSpecify column name and datatype which will be mapped with the property
IndexCreate an Index for specified column. (EF 6.1 onwards only)
ForeignKeySpecify Foreign key property for Navigation property
NotMappedSpecify that property will not be mapped with database
DatabaseGeneratedDatabaseGenerated 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).
InversePropertyInverseProperty is useful when you have multiple relationships between two classes.
ComplexTypeMark 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 N'UPDATE [dbo].[Students]
SET [StudentName] = @0
WHERE (([StudentId] = @1) AND ([StudentName] = @2))
',N'@0 nvarchar(max) ,@1 int,@2 nvarchar(max) ',@0=N'Steve',@1=1,@2=N'Bill'
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; }
   
}

 

转载于:https://www.cnblogs.com/lanpingwang/p/6637482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值