Entity Framework 中 Attribute的整理 来源网络


   
   
  • 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值