C# EF核心急切加载返回null
我有一个实体患者和一个实体用户。他们之间是一对一的关系 用户: 病人:C# EF核心急切加载返回null,c#,entity-framework,asp.net-core,.net-core,C#,Entity Framework,Asp.net Core,.net Core,我有一个实体患者和一个实体用户。他们之间是一对一的关系 用户: 病人: public class Patient { public long Id { get; set; } public long UserId { get; set; } public User User { get; set; } } 当我试图从数据库中提取患者时,用户的引用为空 DataContext: protected override void
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);}
....
}
迁移:
使用者
关于调试
我注意到的一点是,Mysql将这种关系视为一对多
好的,您需要在查询中使用
.ToList()
var patients = _dataContext.Patients.Include(x=>x.User).ToList();
好的,您需要在查询中使用
.ToList()
var patients = _dataContext.Patients.Include(x=>x.User).ToList();
创建两个实体之间的关系时,应设置
DeleteBehavior
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasOne(b => b.User)
.WithOne()
.HasForeignKey<Patient>(b => b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(b=>b.User)
.WithOne()
.HasForeignKey(b=>b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
在创建两个实体之间的关系时,应设置删除行为
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasOne(b => b.User)
.WithOne()
.HasForeignKey<Patient>(b => b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.HasOne(b=>b.User)
.WithOne()
.HasForeignKey(b=>b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}
显然有很多库为该方法提供了功能。包括():Microsoft.EntityFrameworkCore(好的)和System.Data.Entity(坏的)强>
我选择了错误的方法,没有语法错误或运行时错误,只是一个错误的功能。因此显然有一些库为该方法提供了功能。包括():Microsoft.EntityFrameworkCore(好的)和System.Data.Entity(坏的)强>
我选择了错误的,没有语法错误或运行时错误,只是功能不好。我更改了它,但仍然收到空值。我更改了它,仍然收到空值。注释不用于扩展讨论;此对话已结束。评论不用于扩展讨论;这段对话已经结束。
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);
});
{
"id": 1,
"userId": 3,
"user": null
}
var patients = _dataContext.Patients.Include(x=>x.User).ToList();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Patient>()
.HasOne(b => b.User)
.WithOne()
.HasForeignKey<Patient>(b => b.UserId)
.OnDelete(DeleteBehavior.Restrict);
}