1,首先两表关系为1对多,如:
1 public class BUser:AggregateRoot 2 { 3 /// <summary> 4 /// 登陆名称 5 /// </summary> 6 [Required] 7 public string LoginName { get; set; } 8 9 /// <summary> 10 /// 登陆密码 11 /// </summary> 12 [Required] 13 public string Password { get; set; } 14 15 /// <summary> 16 /// 昵称 17 /// </summary> 18 public string NickName { get; set; } 19 20 /// <summary> 21 /// 唯一Key 22 /// </summary> 23 [Required] 24 public string OnlyKey { get; set; } 25 26 /// <summary> 27 /// 注册时间 28 /// </summary> 29 public DateTime AddTime { get; set; } 30 31 /// <summary> 32 /// 用户信息 33 /// </summary> 34 public UserInfo UserInfo { get; set; } 35 36 /// <summary> 37 /// 用户博客类别表 38 /// </summary> 39 public virtual List<UserBlogClazz> UBClazz { get; set; } 40 41 /// <summary> 42 /// 用户博客列表 43 /// </summary> 44 public List<Blogs.Aggregates.Blog> Blogs { get; set; } 45 }
在此中的virtual可不用,该关键字为设置该属性为懒加载。
2,在MAP配置中设置为:
1 public class BUserMap:EntityTypeConfiguration<BUser> 2 { 3 public BUserMap() 4 { 5 this.HasKey(p => p.ID); 6 7 this.ToTable("U_User"); 8 9 this.Property(p => p.ID) 10 .HasColumnName("Id") 11 .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 12 13 HasOptional(p => p.UserInfo) 14 .WithRequired() 15 .Map(p => p.MapKey("Uid")) 16 .WillCascadeOnDelete(true); 17 18 HasOptional(p => p.Blogs) 19 .WithRequired() 20 .Map(p => p.MapKey("Uid")) 21 .WillCascadeOnDelete(true); 22 23 HasMany(p => p.UBClazz) 24 .WithRequired() 25 .Map(p => p.MapKey("Uid")) 26 .WillCascadeOnDelete(true); 27 28 } 29 }
3,在添加信息时,应对BUser中的UBClazz新建。如:
_User.Insert(new Domain.Users.Aggregates.BUser()
{
AddTime = DateTime.Now,
LoginName = "test",
NickName = "test",
OnlyKey = "xxx888",
Password = "123456",
UBClazz = new List<UserBlogClazz>(),
});
该博文为自己的一点心得,大家若觉得有问题可以提出。谢谢!