using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SqlSugar; namespace OrmTest { internal class UnitSameKeyBug { public static void Init() { var db = NewUnitTest.Db; db.CodeFirst.InitTables(typeof(ServiceManage), typeof(UserInfo)); db.DbMaintenance.TruncateTable(); #region 第一次插入数据 int a = db.Insertable(new UserInfo() { ContacAll = new List() { new ContactInfo() { Name="a"} }, Name = "a" }).ExecuteCommand(); int b = db.Insertable(new UserInfo() { ContacAll = new List() { new ContactInfo() { Name = "z" } }, Name = "b" }).ExecuteCommand(); int c = db.Insertable(new ServiceManage() { ProjectName = "ceshi", userId = 1, careId = 2 }).ExecuteCommand(); #endregion var list = db.Queryable() .LeftJoin((s, ss) => s.userId == ss.Id) .LeftJoin((s, ss, sss) => s.careId == sss.Id) .Select((s, ss, sss) => new ServiceManagementViewModel { serviceManage = s, userInfo = ss, careUserInfo=sss }).ToList(); if (!list.First().careUserInfo.ContacAll.Any()) { throw new Exception("unit error"); } db.CodeFirst.InitTables(); db.Insertable(new ContactInfo() { Name = Guid.NewGuid() + "", Relationship = "a", Phone = "aa", Phone2 = "a", Address = "a" }) .ExecuteCommand(); var list2=db.Queryable() .LeftJoin((x, y) => x.Name == y.Name) .Select((x, y) => new UserInfoDTO() { Name=x.Name.SelectAll(), ContacAll=y }).ToList(); var list3 = db.Queryable() .LeftJoin((x, y) => x.Name == y.Name) .Select((x, y) => new UserInfoDTO() { Name = x.Name.SelectAll(), ContacAll = y }).ToListAsync().GetAwaiter().GetResult(); if (list3.First().ContacAll == null || list3.First().ContacAll==null) { throw new Exception("unit error"); } } } public class UserInfo { [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } [SugarColumn(Length = 20, IsNullable = true)] public string Name { get; set; } [SugarColumn(IsJson = true)] public List ContacAll { get; set; } } public class UserInfoDTO { [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } [SugarColumn(Length = 20, IsNullable = true)] public string Name { get; set; } public ContactInfo ContacAll { get; set; } } public class ContactInfo { /// /// 姓名 /// public string Name { get; set; } /// /// 关系 /// public string Relationship { get; set; } /// /// 手机号 /// public string Phone { get; set; } /// /// 备用手机号 /// public string Phone2 { get; set; } /// /// 联系地址 /// public string Address { get; set; } } public class ServiceManagementViewModel { public ServiceManage serviceManage { get; set; } public UserInfo userInfo { get; set; } public UserInfo careUserInfo { get; set; } } public class ServiceManage { [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } [SugarColumn(Length = 20, IsNullable = true)] public string ProjectName { get; set; } public int userId { get; set; } public int careId { get; set; } } }