You need to enable JavaScript to run this app. .NET 5 Web API 如何获取Patient与InPatient表数据映射到ViewInPatient
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5 Web API 如何获取Patient与InPatient表数据映射到ViewInPatient

方案1:直接通过EF Core LINQ查询映射(无需额外创建数据库视图)

步骤1:补全一对一关联配置

你当前OnModelCreating中没有显式配置Patient和InPatient的一对一关系,首先补充配置避免导航属性加载异常:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // 原有DoctorPatient关联配置保留
    modelBuilder.Entity<DoctorPatient>()
        .HasOne(x => x.Doctor)
        .WithMany(dp => dp.DoctorPatients)
        .HasForeignKey(di => di.DoctorId);

    modelBuilder.Entity<DoctorPatient>()
        .HasOne(y => y.Patient)
        .WithMany(dp => dp.DoctorPatients)
        .HasForeignKey(pi => pi.PatientId);
    
    // 新增Patient和InPatient一对一关联配置
    modelBuilder.Entity<Patient>()
        .HasOne(p => p.InPatients)
        .WithOne(i => i.Patient)
        .HasForeignKey<InPatient>(i => i.InPatientId);
}

步骤2:查询并投影到ViewInPatient

直接利用导航属性关联两张表,投影为目标类:

// 如果只需查询有住院记录的患者,用内联逻辑
var inPatientViews = _context.Patients
    .Where(p => p.InPatients != null)
    .Select(p => new ViewInPatient
    {
        Name = p.Name,
        age = p.age,
        Weight = p.Weight,
        Gender = p.Gender,
        Address = p.Address,
        PhoneNo = p.PhoneNo,
        Disease = p.Disease,
        RoomNo = p.InPatients.RoomNo,
        DateOfAddmission = p.InPatients.DateOfAddmission,
        DateOfDischarge = p.InPatients.DateOfDischarge,
        Advance = p.InPatients.Advance,
        LabNo = p.InPatients.LabNo
    }).ToList();

// 如果需要包含所有患者(无住院记录的住院相关字段为null,需先将ViewInPatient中值类型属性设为可空,比如int? Advance、DateTime? DateOfAddmission)
var allPatientViews = _context.Patients
    .Select(p => new ViewInPatient
    {
        Name = p.Name,
        age = p.age,
        Weight = p.Weight,
        Gender = p.Gender,
        Address = p.Address,
        PhoneNo = p.PhoneNo,
        Disease = p.Disease,
        RoomNo = p.InPatients != null ? p.InPatients.RoomNo : null,
        DateOfAddmission = p.InPatients != null ? p.InPatients.DateOfAddmission : default,
        DateOfDischarge = p.InPatients != null ? p.InPatients.DateOfDischarge : default,
        Advance = p.InPatients != null ? p.InPatients.Advance : default,
        LabNo = p.InPatients != null ? p.InPatients.LabNo : null
    }).ToList();

方案2:修复数据库视图实现的问题

你之前视图返回空的核心原因有两个:

  1. EF Core默认生成的数据库表名是实体类名,不是DbSet的属性名:Patient类对应表名Patient,InPatient类对应表名InPatient,所以你选表的时候选InPatient是正确的
  2. 大概率是视图的关联条件写错了,一对一关联的外键是InPatient.InPatientId关联Patient.Id

步骤1:创建正确的SQL视图

在SQL Server中执行以下语句创建视图:

CREATE VIEW ViewInPatient
AS
SELECT 
    p.Name,
    p.age,
    p.Weight,
    p.Gender,
    p.Address,
    p.PhoneNo,
    p.Disease,
    i.RoomNo,
    i.DateOfAddmission,
    i.DateOfDischarge,
    i.Advance,
    i.LabNo
FROM Patient p
INNER JOIN InPatient i ON p.Id = i.InPatientId
-- 如果要包含无住院记录的患者,改成LEFT JOIN

步骤2:配置EF Core支持视图查询

EF Core 3.0+已弃用DbQuery,改用无键实体配置:

  1. 在ApplicationDbContext中添加DbSet:
public DbSet<ViewInPatient> ViewInPatients { get; set; }
  1. 在OnModelCreating中添加无键实体配置:
modelBuilder.Entity<ViewInPatient>().HasNoKey().ToView("ViewInPatient");

配置完成后直接调用_context.ViewInPatients.ToList()即可获取数据。


内容的提问来源于stack exchange,提问作者Aaqil Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 23:57:01