Entity Framework 中 Attribute的整理 来源网络

来源:互联网 发布:国内云计算服务提供商 编辑:程序博客网 时间:2024/06/05 19:49
  • KeyAttribute
  • StringLengthAttribute
  • MaxLengthAttribute
  • ConcurrencyCheckAttribute
  • RequiredAttribute
  • TimestampAttribute   
  • ComplexTypeAttribute
  • ColumnAttribute
  • TableAttribute
  • InversePropertyAttribute
  • ForeignKeyAttribute
  • DatabaseGeneratedAttribute
  • NotMappedAttribute 
http://www.cnblogs.com/zq8024/archive/2012/07/23/2605126.html
Table 指定表名
Ignore 忽略
Key 主键
MaxLength 最大长度
[ForeignKey("Target")] http://www.cnblogs.com/Gyoung/archive/2013/01/22/2869782.html
指定外键
 [Timestamp] 时间戳 http://www.cnblogs.com/jlzhou/archive/2013/03/15/2960759.html
InverseProperty http://www.cnblogs.com/Gyoung/archive/2013/01/22/2869782.html
外键表的属性值要带virtual关键字  假设Destination中的一条记录下有上w条Lodging记录

有个逻辑只需要查Destination表数据 如果不用virtual 那么那w条Lodging记录也会被带进来

如果用virtual 那么只有在访问 Destination.Lodgings属性的时候 才去数据库加载.
http://www.cnblogs.com/Gyoung/archive/2013/01/22/2869782.html
http://www.cnblogs.com/wlflovenet/archive/2011/07/27/EFandMvc5.html
[Required(ErrorMessage = "Title is required.")]
public string FirstMidName { get; set; }
必填项 错误原因
[Column("FirstName")]public string FirstMidName { get; set; }
利用这个特性 我们可以使FirstMidName属性映射到数据库的列为FirstName   在这里 如果使用[MaxLength(10)] 可以规定字段长度 [Required]这个来规定是否允许空 这是比较常用的 
[Display(Name = "Office Location")]
public string Location { get; set; }
字段的显示名称
[Required(ErrorMessage = "Number of credits is required.")][Range(0,5,ErrorMessage="Number of credits must be between 0 and 5.")] public int Credits { get; set; }
控制字段值范围

[Column(TypeName="money")]public decimal? Budget { get; set; }
通过这个特性 我们可以指定 生成到数据库里的列的属性



 [DatabaseGenerated(DatabaseGeneratedOption.None)] [Display(Name = "Number")]public int CourseID { get; set; }

在EF中,我们建立数据模型的时候,可以给属性配置数据生成选项DatabaseGenerated,它后有三个枚举值:Identity、None和Computed。

Identity:自增长

None:不处理

Computed:表示这一列是计算列。

在EF中,如果主键是int类型,Code First生成数据库的时候会自动设置该列为自增长。但如果主键是Guid类型,我们就要手动的去设置了。

对于下面的模型,如果我们没有设置自增长,数据库中会以0来填充

http://www.cnblogs.com/Gyoung/archive/2013/01/18/2865998.html

 [DisplayFormat(DataFormatString="{0:c}")]        [Required(ErrorMessage = "Budget is required.")]        [Column(TypeName="money")]        public decimal? Budget { get; set; }        [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]        [Required(ErrorMessage = "Start date is required.")]        public DateTime StartDate { get; set; }
对数据进行格式化

http://coding.windstyle.cn/sharepoint-entity-framework-2-attributes/
http://www.cnblogs.com/xishuai/p/3712361.html
http://www.cnblogs.com/mecity/archive/2011/07/08/2099853.html
0 0
原创粉丝点击