【发布时间】:2020-08-01 06:16:34
【问题描述】:
我目前正在使用 .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; }
}
数据库上下文
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"
}
这种情况发生在其他具有类似结构的地方。
谁能告诉我是什么原因造成的以及如何解决这个问题?
【问题讨论】:
-
为什么会有这些
[NotMapped]属性? -
您的
fkRefereeId列包含重复项,因此关系不是一对一的。 -
@GertArnold 这听起来可能很傻,但我想强调一下,它将把它排除在数据库映射之外,同时提醒自己或将来维护的人。
-
@IvanStoev 非常感谢!它在修改 dbcontext 关系和 User 类中的属性后起作用。我几乎忘记了一对一的关系是如何运作的!再次,非常感谢!
标签: .net-core entity-framework-core eager-loading .net-core-3.0 webapi