EF初级入门 (一对多多对多)

Posted JustDo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF初级入门 (一对多多对多)相关的知识,希望对你有一定的参考价值。

1、创建新项目并引入Entity Framework

 

然后就可以开始使用EF了

 

开始使用一对多

Users 用户类,UserDetail用户详情类:一个用户对应多个详情(仅用于测试)

 1 public class UserDetail
 2     {
 3         public int DetailId { get; set; }
 4         public string NAME { get; set; }
 5         public int uid { get; set; }
 6         public string remark { get; set; }
 7         public virtual Users user { get; set; }
 8     }
 9     public class Users
10     {
11         public int id { get; set; }
12         public string Name { get; set; }
13         public int Age { get; set; }
14         public DateTime Birth { get; set; }
15         public virtual ICollection<UserDetail> detail { get; set; }
16 
17     }
View Code

ObjectContext类:

 1 public class ObjectContext : DbContext
 2     {
 3         public ObjectContext()
 4             : base("server=.;uid=xx;pwd=xxx;database=XXX")
 5         {
 6 
 7         }
 8         protected override void OnModelCreating(DbModelBuilder modelBuilder)
 9         {
10             base.OnModelCreating(modelBuilder);
11             modelBuilder.Entity<Users>().HasKey(p => p.id);
12             modelBuilder.Entity<UserDetail>().ToTable("UserDetail");
13             modelBuilder.Entity<UserDetail>().HasKey(p => p.DetailId);
14             modelBuilder.Entity<UserDetail>().Property(p => p.NAME).HasColumnName("NAME");
15             modelBuilder.Entity<UserDetail>().HasRequired(p => p.user).WithMany(p => p.detail).HasForeignKey(p => p.uid);
16         }
17         public DbSet<Users> users { get; set; }
18         public DbSet<UserDetail> userdetail { get; set; }
19 
20     }
View Code

查询方法:

 1 public void GetAll()
 2         {
 3             using (ObjectContext db = new ObjectContext())
 4             {
 5                 var users = (from a in db.users select a).ToList<Users>();
 6                 for (int i = 0; i < users.Count; i++)
 7                 {
 8                     Console.WriteLine("ID:"+users[i].id+",Name:"+users[i].Name);
 9                     foreach (var item in users[i].detail)
10                     {
11                         Console.WriteLine("  id:" + item.DetailId + ",NAME" + item.NAME);
12                     }
13                 }
14             }
15         }
View Code

删除和修改子集同时进行方法

 1 public void Update()
 2 {
 3 using (ObjectContext db = new ObjectContext())
 4             {
 5                 var users = (from a in db.users where a.id == 1 select a).First();
 6                 //删除子集
 7                 var detailitem=users.detail.Where(p=>p.DetailId==7).FirstOrDefault();
 8                 db.Entry(detailitem).State = System.Data.Entity.EntityState.Deleted;
 9                 users.detail.Remove(detailitem);
10                 //修改子集
11                 var detailitem2 = users.detail.Where(p=>p.DetailId==2).FirstOrDefault();
12                 detailitem2.NAME = "这是修改后的数据";
13                 db.Entry(detailitem2).State = System.Data.Entity.EntityState.Modified;
14                 db.SaveChanges();
15             }
16 }
View Code

然后是对多对

新增一个角色类 Roles

 1 public class Roles
 2     {
 3         public Roles()
 4         {
 5             users = new HashSet<Users>();
 6         }
 7         public int RoleID { get; set; }
 8         public string RoleName { get; set; }
 9         public string Remark { get; set; }
10         public virtual ICollection<Users> users { get; set; }
11     }
View Code

 

修改ObjectContext类:

1 modelBuilder.Entity<Roles>().HasKey(p => p.RoleID);
2             //modelBuilder.Entity<UserRoles>().HasKey(p => p.ID);
3             modelBuilder.Entity<Roles>().HasMany(p => p.users).WithMany(p => p.roles).Map(
4                 p =>
5                 {
6                     p.MapLeftKey("RoleId");
7                     p.MapRightKey("UserId");
8                     p.ToTable("UserRoles");
9                 });
View Code

 

写测试方法:

 1 public static void AddUserRole()
 2         {
 3             using (ObjectContext db = new ObjectContext())
 4             {
 5                 //添加一个用户 两个角色
 6                 var user = new Users() { Name = "Test用户1", Age = 20, Birth = DateTime.Now };
 7                 var role1 = new Roles() { RoleName = "Test角色1" };
 8                 var role2 = new Roles() { RoleName = "Test角色2" };
 9                 user.roles.Add(role1);
10                 user.roles.Add(role2);
11                 db.users.Add(user);
12                 db.SaveChanges();
13             }
14         }
View Code
查询:
 1 public static void GetAllUserRoles()
 2         {
 3             using (ObjectContext db = new ObjectContext())
 4             {
 5                 var users = (from a in db.users select a).ToList<Users>();
 6                 if (users != null)
 7                 {
 8                     Console.WriteLine("User查询结果:");
 9                     foreach (var item in users)
10                     {
11                         Console.WriteLine("UserID:" + item.id + ",UserName:" + item.Name + ",Birth:" + item.Birth);
12                         if (item.roles != null)
13                         {
14                             foreach (var role in item.roles)
15                             {
16                                 Console.WriteLine("  RoleId:" + role.RoleID + ",RoleNmae:" + role.RoleName + ",UserId:" + item.id);
17                             }
18                         }
19                     }
20                 }
21                 var roles = (from a in db.roles select a).ToList<Roles>();
22                 if (roles != null)
23                 {
24                     Console.WriteLine("Role查询结果:");
25                     foreach (var item in roles)
26                     {
27                         Console.WriteLine("RoleID:" + item.RoleID + ",RoleName:" + item.RoleName);
28                         if (item.users != null)
29                         {
30                             foreach (var user in item.users)
31                             {
32                                 Console.WriteLine("  UserID:" + user.id + ",UserName:" + user.Name + ",Birth:" + user.Birth + ",RoleID:" + item.RoleID);
33                             }
34                         }
35                     }
36                 }
37             }
38         }
View Code

 

然后写完测试。。。

源代码:EFDemo

 
 
 

以上是关于EF初级入门 (一对多多对多)的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis入门---一对多多对多

Hibernate入门—— 一对多多对多关系

hibernate多对多 一对多 及简单入门

mybatis映射 一对一一对多多对多高级映射

mybatis 详解------一对一对多多对多

mybatis 一对一对多多对多对多