EntityFramework:FluentAPI关系映射
1.表的设计
2.关系映射的方法
基本套路 this.Has***(x=>x.aaa).with***() 当前这个表的和aaa 属性的表的关系是has定义,with定义的是aaa表和这个表的关系
HasOptional() 有一个可以选(可以为空)
HasRequired() 有一个必须选(不能为空)
HasMany() 有很多的
WithOptional() 可选的
WithRequired() 必须的
WithMany() 很多的
3.创建一个DbContext类
public class FluentAPIContext : DbContext { public FluentAPIContext() : base("name=Conn") { } public virtual DbSet<User> User { get; set; } public virtual DbSet<Role> Role { get; set; } public virtual DbSet<Action> Action { get; set; } public virtual DbSet<UserAction> UserAction { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly()); } }
4.Model 类
public class User { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Role> Roles { get; set; } = new List<Role>(); public virtual ICollection<UserAction> UserActions { get; set; } = new List<UserAction>(); }
public class Role { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<User> Users { get; set; } = new List<User>(); }
public class Action { public int Id { get; set; } public string Name { get; set; } }
public class UserAction { public int Id { get; set; } public int UserId { get; set; } public virtual User User { get; set; } public int ActionId { get; set; } public virtual Action Action { get; set; } public bool HasPermisssin { get; set; } }
5.配置类
public class UserMap : EntityTypeConfiguration<User> { public UserMap() { this.ToTable("sys_user"); //多对多 this.HasMany(x => x.Roles).WithMany().Map(x => x.ToTable("sys_user_role").MapLeftKey("UserId").MapRightKey("RoleId")); //双向设置,一般不介意这样设置,一般一对多只要设置多的一方,或者说这里不用设置。 this.HasMany(x => x.UserActions).WithRequired().HasForeignKey(x => x.UserId); } }
public class RoleMap : EntityTypeConfiguration<Role> { public RoleMap() { this.ToTable("sys_role"); } }
public UserActionMap() { this.ToTable("sys_user_action"); //一对多 this.HasRequired(x => x.User).WithMany().HasForeignKey(x => x.UserId); this.HasRequired(x => x.Action).WithMany().HasForeignKey(x => x.ActionId); }
6.测试代码
class Program { static void Main(string[] args) { using (FluentAPIContext ctx = new FluentAPIContext()) { //User user = new User(); //user.Name = "admin2"; //ctx.User.Add(user); //Action action = new Action(); //action.Name = "修改"; //ctx.Action.Add(action); //UserAction userAction = new UserAction(); //userAction.User = user; //userAction.Action = action; //userAction.HasPermisssin = false; //ctx.UserAction.Add(userAction); //ctx.SaveChanges(); //User user = ctx.User.First(); //foreach (UserAction userAction in user.UserActions) //{ // Console.WriteLine(userAction.HasPermisssin); //} User u1 = new User(); u1.Name = "admin"; User u2 = new User(); u1.Name = "ordinary"; Role r1 = new Role(); r1.Name = "管理员"; Role r2 = new Role(); r2.Name = "普通会员"; u1.Roles.Add(r1); u2.Roles.Add(r2); ctx.User.Add(u1); ctx.User.Add(u2); ctx.SaveChanges(); } } }
7.一对多关系配置简单例子
this.HasRequired(x => x.User).WithMany().HasForeignKey(x => x.UserId);
8.多对多关系配置简单例子
this.HasMany(x => x.Roles).WithMany().Map(x => x.ToTable("sys_user_role").MapLeftKey("UserId").MapRightKey("RoleId"));