关键词汇解释
required / optional | 必须 / 可空 |
principal / dependent | 表示哪个是主键表 / 表示哪个是外键表 |
hasrequired / withrequired | has表示的是正向的关系,则with表示的是反向关系 |
数据加载
如果用virtual关键字,则标识为该属性会懒加载(第一次用的时候加载)
如果不用virtual关键字,则需要用include主动标识立即加载
可以关闭懒加载:this.Configuration.LazyLoadingEnabled = false;
一对一或一对零关系
1: public class user
2: {
3: public int id { get; set; }
4: public string code { get; set; }
5: public string name { get; set; }
6: }
7: public class dep
8: {
9: public int id { get; set; }
10: public virtual user user { get; set; }
11: }
12: public class MyContext : DbContext
13: {
14: protected override void OnModelCreating(DbModelBuilder modelBuilder)
15: {
16: modelBuilder.Entity<dep>()
17: .HasRequired(d => d.user)
18: .WithOptional();
19: }
20: }
说明:dep里user不能为空,user里不包含dep的导航
如果改为:
1: modelBuilder.Entity<dep>()
2: .HasRequired(d => d.user)
3: .WithRequiredDependent();
则:两边都不能为空
多对多关系
1: public class user
2: {
3: public int id { get; set; }
4: public string code { get; set; }
5: public string name { get; set; }
6: public virtual ICollection<dep> Deps { get; set; }
7: }
8: public class dep
9: {
10: public int id { get; set; }
11: public virtual ICollection<user> Users { get; set; }
12: }
13: public class MyContext : DbContext
14: {
15: protected override void OnModelCreating(DbModelBuilder modelBuilder)
16: {
17: modelBuilder.Entity<dep>()
18: .HasMany(d => d.Users)
19: .WithMany(user => user.Deps);
20: }
21: }
这个简单,不解释
一对多关系
1: public class user
2: {
3: public int id { get; set; }
4: public string code { get; set; }
5: public string name { get; set; }
6: public int DepId { get; set; }
7: public virtual dep Dep { get; set; }
8: }
9: public class dep
10: {
11: public int id { get; set; }
12: public virtual ICollection<user> Users { get; set; }
13: }
14: public class MyContext : DbContext
15: {
16: protected override void OnModelCreating(DbModelBuilder modelBuilder)
17: {
18: modelBuilder.Entity<user>()
19: .HasRequired(u => u.Dep)
20: .WithMany(d => d.Users)
21: .HasForeignKey(u => u.DepId)
22: .WillCascadeOnDelete(false);
23: }
24: }
说明:dep里包含多个user,user里包含一个dep,显示指定外键列为DepId(如果不指定,自动生成的数据库里外键列为Dep_id);
WillCascadeOnDelete(false)表示不级联删除
组合外键
1: public class user
2: {
3: public int id { get; set; }
4: public string code { get; set; }
5: public string name { get; set; }
6: public int DepId { get; set; }
7: public string DepCode { get; set; }
8: public virtual dep Dep { get; set; }
9: }
10: public class dep
11: {
12: public int id { get; set; }
13: public string code { get; set; }
14: public virtual ICollection<user> Users { get; set; }
15: }
16: public class MyContext : DbContext
17: {
18: protected override void OnModelCreating(DbModelBuilder modelBuilder)
19: {
20: modelBuilder.Entity<dep>().HasKey(d => new { d.id, d.code });
21:
22: modelBuilder.Entity<user>()
23: .HasRequired(c => c.Dep)
24: .WithMany(d => d.Users)
25: .HasForeignKey(d => new { d.DepId, d.DepCode });
26:
27: }
28: }
简单不解释
重命名外键列
1: modelBuilder.Entity<user>()
2: .HasRequired(c => c.Dep)
3: .WithMany(t => t.Users)
4: .Map(m => m.MapKey("NewUserID"));