预加载仅出现在某些结果中
Eager loading only appear in some result
我目前正在使用 .Net Core 3.0 并且正在构建 Web API。
我正在尝试将一些相关数据加载到前端。
我有以下 类 和 dbcontext 设置。
public class EntityCommon
{
public int Id { get; set; }
public DateTime CreatedDatetime { get; set; }
}
public class UserAmountClaim: EntityCommon
{
public int UserId { get; set; }
public AmountClaimType Type { get; set; }
public int Amount{ get; set; }
public int? RefereeId { get; set; }
public AmountClaimStatus Status { get; set; }
public DateTime? ClaimedDatetime { get; set; }
[NotMapped]
public virtual User Referee { get; set; }
}
public class User : EntityCommon
{
public string Name { get; set; }
public string Password { get; set; }
public UserStatus Status { get; set; }
[NotMapped]
public virtual UserAmountClaim UserAmountClaim { get; set; }
}
DbContext
modelbuilder.Entity<UserAmountClaim>().ToTable("UserAmountClaim ", "dbo");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.UserId).HasColumnName("fkUserId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.RefereeId).HasColumnName("fkRefereeId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.Type).HasConversion(new EnumToStringConverter<UserAmountClaim>());
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.CreatedDatetime).ValueGeneratedOnAdd();
modelbuilder.Entity<UserAmountClaim>().HasOne(ucc => ucc.Referee).WithOne(user => user.UserAmountClaim);
Startup.cs
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
还有如下数据
Id fkUserId Type Amount fkRefereeId Status ClaimedDateTime CreatedDatetime
52 1 ReferralCommission 100 2 6 NULL 2020-04-18 15:19:34.203
53 1 ReferralCommission 100 2 6 NULL 2020-04-18 15:19:40.343
54 1 ReferralCommission 100 1 6 NULL 2020-04-18 15:36:44.017
55 1 ReferralCommission 100 1 6 NULL 2020-04-18 15:51:31.757
但是当我执行下面的代码时
var result = _dbContext.UserAmountClaim.Where(x => x.UserId == userId &&
x.Status == AmountClaimStatus.PendingUser &&
x.Type == AmountClaimType.ReferralCommission)
.Include(x => x.Referee)
.ToListAsync();
第一项和第三项缺少裁判
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"status": "PendingUser",
"id": 52,
"createdDatetime": "2020-04-18T15:19:34"
},
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"refereeId": 2,
"status": "PendingUser",
"referee": {
"id": 2,
"name": "82109380918",
"password": "",
"status": "Valid",
"createdDatetime": "2020-04-16T17:45:31",
},
"id": 53,
"createdDatetime": "2020-04-18T15:19:40"
},
{
"userId": 1,
"type": "ReferralCommission",
"credit": 100,
"status": "PendingUser",
"id": 54,
"createdDatetime": "2020-04-18T15:36:44"
},
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"refereeId": 1,
"status": "PendingUser",
"referee": {
"id": 1,
"name": "31829389031",
"password": "",
"status": "Valid",
"createdDatetime": "2020-04-16T17:45:31",
},
"id": 54,
"createdDatetime": "2020-04-18T15:36:44"
}
其他具有类似结构的地方都在发生这种情况。
谁能告诉我这是什么原因以及如何解决这个问题?
正如@IvanStoev 指出的那样,建立关系是错误的,属性 class
修改以下对我有用。
public class User : EntityCommon
{
public string Name { get; set; }
public string Password { get; set; }
public UserStatus Status { get; set; }
[NotMapped]
public virtual List<UserAmountClaim> UserAmountClaim { get; set; }
}
DbContext
modelbuilder.Entity<UserAmountClaim>().ToTable("UserAmountClaim ", "dbo");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.UserId).HasColumnName("fkUserId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.RefereeId).HasColumnName("fkRefereeId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.Type).HasConversion(new EnumToStringConverter<UserAmountClaim>());
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.CreatedDatetime).ValueGeneratedOnAdd();
modelbuilder.Entity<UserAmountClaim>().HasOne(ucc => ucc.Referee).WithMany(user => user.UserAmountClaim);
非常感谢@IvanStoev
我目前正在使用 .Net Core 3.0 并且正在构建 Web API。 我正在尝试将一些相关数据加载到前端。
我有以下 类 和 dbcontext 设置。
public class EntityCommon
{
public int Id { get; set; }
public DateTime CreatedDatetime { get; set; }
}
public class UserAmountClaim: EntityCommon
{
public int UserId { get; set; }
public AmountClaimType Type { get; set; }
public int Amount{ get; set; }
public int? RefereeId { get; set; }
public AmountClaimStatus Status { get; set; }
public DateTime? ClaimedDatetime { get; set; }
[NotMapped]
public virtual User Referee { get; set; }
}
public class User : EntityCommon
{
public string Name { get; set; }
public string Password { get; set; }
public UserStatus Status { get; set; }
[NotMapped]
public virtual UserAmountClaim UserAmountClaim { get; set; }
}
DbContext
modelbuilder.Entity<UserAmountClaim>().ToTable("UserAmountClaim ", "dbo");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.UserId).HasColumnName("fkUserId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.RefereeId).HasColumnName("fkRefereeId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.Type).HasConversion(new EnumToStringConverter<UserAmountClaim>());
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.CreatedDatetime).ValueGeneratedOnAdd();
modelbuilder.Entity<UserAmountClaim>().HasOne(ucc => ucc.Referee).WithOne(user => user.UserAmountClaim);
Startup.cs
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters.Add(new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" });
options.SerializerSettings.Converters.Add(new StringEnumConverter());
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
还有如下数据
Id fkUserId Type Amount fkRefereeId Status ClaimedDateTime CreatedDatetime
52 1 ReferralCommission 100 2 6 NULL 2020-04-18 15:19:34.203
53 1 ReferralCommission 100 2 6 NULL 2020-04-18 15:19:40.343
54 1 ReferralCommission 100 1 6 NULL 2020-04-18 15:36:44.017
55 1 ReferralCommission 100 1 6 NULL 2020-04-18 15:51:31.757
但是当我执行下面的代码时
var result = _dbContext.UserAmountClaim.Where(x => x.UserId == userId &&
x.Status == AmountClaimStatus.PendingUser &&
x.Type == AmountClaimType.ReferralCommission)
.Include(x => x.Referee)
.ToListAsync();
第一项和第三项缺少裁判
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"status": "PendingUser",
"id": 52,
"createdDatetime": "2020-04-18T15:19:34"
},
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"refereeId": 2,
"status": "PendingUser",
"referee": {
"id": 2,
"name": "82109380918",
"password": "",
"status": "Valid",
"createdDatetime": "2020-04-16T17:45:31",
},
"id": 53,
"createdDatetime": "2020-04-18T15:19:40"
},
{
"userId": 1,
"type": "ReferralCommission",
"credit": 100,
"status": "PendingUser",
"id": 54,
"createdDatetime": "2020-04-18T15:36:44"
},
{
"userId": 1,
"type": "ReferralCommission",
"amount": 100,
"refereeId": 1,
"status": "PendingUser",
"referee": {
"id": 1,
"name": "31829389031",
"password": "",
"status": "Valid",
"createdDatetime": "2020-04-16T17:45:31",
},
"id": 54,
"createdDatetime": "2020-04-18T15:36:44"
}
其他具有类似结构的地方都在发生这种情况。
谁能告诉我这是什么原因以及如何解决这个问题?
正如@IvanStoev 指出的那样,建立关系是错误的,属性 class
修改以下对我有用。
public class User : EntityCommon
{
public string Name { get; set; }
public string Password { get; set; }
public UserStatus Status { get; set; }
[NotMapped]
public virtual List<UserAmountClaim> UserAmountClaim { get; set; }
}
DbContext
modelbuilder.Entity<UserAmountClaim>().ToTable("UserAmountClaim ", "dbo");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.UserId).HasColumnName("fkUserId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.RefereeId).HasColumnName("fkRefereeId");
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.Type).HasConversion(new EnumToStringConverter<UserAmountClaim>());
modelbuilder.Entity<UserAmountClaim>().Property(ucc => ucc.CreatedDatetime).ValueGeneratedOnAdd();
modelbuilder.Entity<UserAmountClaim>().HasOne(ucc => ucc.Referee).WithMany(user => user.UserAmountClaim);
非常感谢@IvanStoev