EF实体创建规范
1 说明
为了在界面操作和数据操作方便,整理出EF实体创建规范。后续持续进行文件更新。
(以下特性生成使用MVC4+EF6.1.3)
2 数据库特性描述
2.1 Key
| [Key] public int ID { get; set; }
|
2.2 外键,注意外键和外键表的主键类型要一致。
| public Identity Identity { get; set; } [ForeignKey("Identity")] public int IdentityID { get; set; }
|
2.3 唯一,创建唯一索引,只支持EF6.0以上版本。
| [Index(IsUnique = true)] public string OrderName { get; set; }
|
2.4 字符串长度限制
|
[Key] public string DefaultLength { get; set; } public string DefaultLength2 { get; set; }
[Display(Name = "最大长度限制"), MaxLength(10)] public string MaxLength { get; set; }
[Display(Name = "最小长度"), MinLength(10)] public string MinLength { get; set; }
[Display(Name = "固定长度"), StringLength(10)] public string StringLength { get; set; }
[Display(Name = "长度范围"), Range(10, 20)] public string StringRange{ get; set; }
|
由此可以看出字符串生成的数据库字段:
1) DefaultLength,DefaultLength2,主键字符默认长度为128。普通字段字符串默认长度为max。
2) MaxLength设置有效和StringLength设置效果相同。
3) MinLength和Range设置无效。
2.5 需要或必须字段
|
[Required] public string Name { get; set; } public string Name2 { get; set; }
|
3 引用、聚合组合
3.1 引用
3.1.1 模型建立,在事例中创建了一个Hero(英雄)类,引用Areas(大区)和Identities(人员认证)类。
| /// <summary> /// 英雄 /// </summary> public class Hero { …(此处省略1万字) [ForeignKey("Identity")] public int IdentityID { get; set; } public Identity Identity { get; set; }
[ForeignKey("Area")] public int AreaID { get; set; } public virtual Area Area { get; set; } } /// <summary> /// 游戏大区 /// </summary> public class Area { public int ID { get; set; } public string Location { get; set; } } /// <summary> /// 认证 /// </summary> public class Identity { public int ID { get; set; } public string UserName { get; set; } }
|
3.1.2 不知道细心的同学注意没有,在Hero中的Area中有virtual,而在Identities中并没有virtual。
这里引出懒加载(LazyLoading)的问题,其实这个应该是从设置中去理解这个问题。如果该引用为强引用,在使用Hero时一定会用到Area则添加virtual,添加virtual之后,只要查询Hero就会联合查询出Area信息。如果为弱引用,在使用Hero时可能不使用Identities则不添加virtual。
测试:
1) 添加Areas数据
2) 添加Identities数据
3) 添加Hero数据
4) 将Hero中ID等于1的寒冰妹子查询出来。
|
using (AppDbContext dbc = new AppDbContext()) { var hero = dbc.Heros.Where(h => h.ID == 1).SingleOrDefault(); }
|
PS:经过测试EF5.0同样如此。
3.2 聚合组合(点我查看)
3.2.1 模型建立,在事例中创建了一个Hero(英雄)类,英雄有多个皮肤Face 和多个技能Skill。
(注意,在Skill类中和Face类中都可以添加对于Hero的外键,这样便于数据操作。)
|
/// <summary> /// 英雄 /// </summary> public class Hero { …(此处省略1万字) /// <summary> /// 技能集合 /// </summary> public virtual ICollection<Skill> Skills { get; set; }
/// <summary> /// 皮肤集合 /// </summary> public ICollection<Face> Faces { get; set; } } /// <summary> /// 技能 /// </summary> public class Skill { public int ID { get; set; } public string Name { get; set; } public int HeroID { get; set; }
[ForeignKey("HeroID")] public Hero Hero { get; set; } } /// <summary> /// 英雄皮肤 /// </summary> public class Face { public int ID { get; set; } public string Name { get; set; } public int HeroID { get; set; }
[ForeignKey("HeroID")] public Hero Hero { get; set; } }
|
3.2.2 同样从Hero中可以看出Skills添加了virtual,属于组合关系,而Faces没有添加属于聚合关系。组合关系(添加了virtual)在使用Hero时就加载了Skills数据。而聚合(未添加virtual)就没有加载数据,进行了懒加载(LazyLoading)。
测试:
1) 添加Face数据。
2) 添加Skill数据。
3) 将Hero中ID等于1的寒冰妹子查询出来。
|
using (AppDbContext dbc = new AppDbContext()) { var hero = dbc.Heros.Where(h => h.ID == 1).SingleOrDefault(); }
|
本文介绍了使用EF6.1.3创建实体的规范,包括主键、外键、唯一索引等特性配置方法,并通过示例详细解释了引用与聚合组合的区别及应用。
228

被折叠的 条评论
为什么被折叠?



