• DataAnnotation特性列举


    DataAnnotation 特性由.NET 3.5中引进,给.NET中的类提供了一种添加验证的方式。
    DataAnnotation由命名空间System.ComponentModel.DataAnnotations提供。下面列举实体模型中常用的DataAnnotation特性:

    KeyAttribute:对数据库中表的主键的设置
    [Key]
    public int OrderID { get; set; }

    RequiredAttribute:对应数据库中字段的数据是否可以为null
    [Required]
    public string OrderName { get; set; }

    MaxLengthAttribute:对应数据库中字符串类型字段的最大长度
    [MaxLength(60)]
    public string Employee{get;set;}

    MinLengthAttribute:在数据库中无对应,但在代码中字符串最小长度
    [MaxLength(60),MinLength(10)] 
    public string Employee{get;set;}

     

    ConcurrencyCheckAttribute:指定用于开放式并发检查的列的数据类型
    [ConcurrencyCheck]
    public string Address { get; set; }

    TimestampAttribute:将列的数据类型指定为行版本
    [Timestamp]
    public byte[] TimeStamp { get; set; }

    System.ComponentModel.DataAnnotations命名空间中只定义了部分实体验证的特性,在EntityFramework程序集中定义了更多的数据映射特性

    DatabaseGeneratedAttribute:标记指定实体属性是由数据库生成的,并指定生成策略(None数据库不生成值,Identity当插入行时,数据库生成值,Computed当插入或更新行时,数据库生成值)
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreateDate { get; set; }

    ColumnAttribute:指定实体属性在数据库中的列名及数据类型
    [Column("Notes", TypeName = "ntext")]
    public string Note { get; set; }

    TableAttribute:指定实体类对应的数据表名
    [Table("Order",Schema="Order")]
    public class Order

    ForeignKeyAttribute:指定导航属性的外键字段
    public class Customer
    {
        public int ID { get; set; }
     
        public string Name { get; set; }
    }
    [ForeignKey("ID")]
    public Customer customer { get; set; }

    NotMappedAttribute:标记指定实体属性在创建数据库中不创建对应字段
    [NotMapped]
    public string PhotoPath { get; set; }

    ComplexTypeAttribute:标记指定实体属性是将一个对象作为另一个对象的属性,映射到数据库中则子对象表现为多个属性字段
    [ComplexType]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
        public Name Name { get; set; }

  • 相关阅读:
    CodeForces
    CodeForces
    AtCoder
    AtCoder
    CodeForces
    CodeForces
    CodeForces
    CodeForces
    Centos7配置yum国内镜像及仓库升级
    环境变量
  • 原文地址:https://www.cnblogs.com/ljdong7/p/12612691.html
Copyright © 2020-2023  润新知