标签:
一对一
就拿后台用户权限相关的实体来说明吧,用户表,用户详细表,是一对一的关系:
1 /// <summary> 2 /// 用户信息类 3 /// </summary> 4 public class User 5 { 6 public string UserId { get; set; } 7 public string UserName { get; set; } 8 public string Password { get; set; } 9 public UserStatus Status { get; set; } 10 public DateTime CreateTime { get; set; } 11 //public string UserDetailId { get; set; } 12 public UserDetail UserDetail { get; set; } 13 }
public UserDetail UserDetail { get; set; }
/// <summary> /// 用户详细信息类 /// </summary> public class UserDetail { public string UserDetailId { get; set; } public string Name { get; set; } public string Age { get; set; } public int Sex { get; set; } public User User { get; set; } }
modelBuilder.Entity<UserDetail>().HasRequired(u => u.User);
modelBuilder.Entity<User>().HasRequired(u => u.UserDetail).WithMany().HasForeignKey(u => u.UserDetailId);
public class Role { public string RoleId { get; set; } public string RoleName { get; set; } public List<User> Users { get; set; } }
modelBuilder.Entity<Role>().HasMany(u => u.Users).WithRequired().Map(u => u.MapKey("RoleId"));
public IList<Menu> Menus { get; set; }
public class Menu { public string MenuId { get; set; } public string MenuName { get; set; } public string Order { get; set; } public bool Enable { get; set; } public string AbsPath { get; set; } public IList<Role> Roles { get; set; } }
modelBuilder.Entity<Role>().HasMany(u => u.Menus).WithMany(u => u.Roles).Map(u => { u.MapLeftKey("MenuId"); //设置左表字段名称 u.MapRightKey("RoleId"); //设置右表字段名称 u.ToTable("Role_Menu"); //设置表名称 });
标签:
原文地址:http://www.cnblogs.com/yangda/p/4832949.html