[英]EF Core Eager loading returns null
我有一个实体患者和一个实体用户。 他们之间是一对一的关系。
用户:
public class User
{
public long Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
......
[JsonIgnore]
public Patient Patient { get; set; }
}
病人:
public class Patient
{
public long Id { get; set; }
public long UserId { get; set; }
public User User { get; set; }
}
当我尝试从数据库中提取患者时,我得到了该用户的空引用。
DataContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasOne(b => b.User)
.WithOne(i => i.Patient)
.HasForeignKey<Patient>(b => b.UserId);}
....
}
患者回复:
public IEnumerable<Patient> GetAll()
{
var patients = _dataContext.Patients.Include(x=>x.User);
return patients;
}
控制器方法:
[HttpGet("patients")]
public IActionResult GetAllPatient()
{
return Ok(_patientRepository.GetAll());
}
迁移:用户
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
Name = table.Column<string>(nullable: true),
Username = table.Column<string>(nullable: true),
PasswordHash = table.Column<byte[]>(nullable: true),
PasswordSalt = table.Column<byte[]>(nullable: true),
BirthDate = table.Column<DateTime>(nullable: false),
GenderId = table.Column<int>(nullable: true),
UserRoleId = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
table.ForeignKey(
name: "FK_Users_Genders_GenderId",
column: x => x.GenderId,
principalTable: "Genders",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Users_UserRoles_UserRoleId",
column: x => x.UserRoleId,
principalTable: "UserRoles",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
耐心
migrationBuilder.CreateTable(
name: "Patients",
columns: table => new
{
Id = table.Column<long>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
UserId = table.Column<long>(nullable: false),
DoctorId = table.Column<long>(nullable: true),
CaregiverId = table.Column<long>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Patients", x => x.Id);
table.ForeignKey(
name: "FK_Patients_Caregivers_CaregiverId",
column: x => x.CaregiverId,
principalTable: "Caregivers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Patients_Doctors_DoctorId",
column: x => x.DoctorId,
principalTable: "Doctors",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Patients_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
我在 Postman 上收到的:
{
"id": 1,
"userId": 3,
"user": null
}
您应该在创建两个实体之间的关系时设置DeleteBehavior
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasOne(b => b.User)
.WithOne()
.HasForeignKey<Patient>(b => b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
显然,有一些库为方法 .Include() 提供了功能: Microsoft.EntityFrameworkCore(好的)和System.Data.Entity(坏的); 我选择了坏的,我没有语法错误或运行时错误,只是一个糟糕的功能。
好的,您需要在查询中使用.ToList()
。
var patients = _dataContext.Patients.Include(x=>x.User).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.