【问题标题】:Get All data from two tables in .net 5 web api?从.net 5 web api中的两个表中获取所有数据?
【发布时间】:2021-09-21 08:41:53
【问题描述】:

Patient.cs //这是患者模型类

namespace HMS.Models
{
public class Patient
{
    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
    public int age { get; set; }
    public int Weight { get; set; }
    public string Gender { get; set; }
    public string Address { get; set; }
    public string PhoneNo { get; set; }
    public string Disease { get; set; }

    [JsonIgnore]
    public IList<DoctorPatient> DoctorPatients { get; set; }
    public InPatient InPatients { get; set; }
    public OutPatient OutPatients { get; set; }
  }
}

InPatient.cs //这个 InPatient 模型类

namespace HMS.Models
{
  public class InPatient
  {
    [ForeignKey("Patient")]
    public string InPatientId { get; set; }
    public string RoomNo { get; set; }
    public DateTime DateOfAddmission { get; set; }
    public DateTime DateOfDischarge { get; set; }
    public int Advance { get; set; }
    public string LabNo { get; set; }


    public Patient Patient { get; set; }
  }
}

这里Patient和InPatient Attribute是一一对应的

ViewInPatient.cs

namespace HMS.Models
{
  public class ViewInPatient
{
    public string Name { get; set; }
    public int age { get; set; }
    public int Weight { get; set; }
    public string Gender { get; set; }
    public string Address { get; set; }
    public string PhoneNo { get; set; }
    public string Disease { get; set; }
    public string RoomNo { get; set; }
    public DateTime DateOfAddmission { get; set; }
    public DateTime DateOfDischarge { get; set; }
    public int Advance { get; set; }
    public string LabNo { get; set; }
}
}

这是我的 DbContext 类

public class ApplicationDbContext:DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options):base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        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);
    }
    public DbSet<Patient> Patients { get; set; }
    public DbSet<Doctor> Doctors { get; set; }
    public DbSet<DoctorPatient> DoctorPatients { get; set; }
    public DbSet<InPatient> InPatients  { get; set; }

    //public DbQuery<ViewInPatient> ViewInPatients { get; set; }

}

如何像 ViewInPatient 类一样获取患者和 InPatients 表的所有数据? (我试图在 sql server 中创建一个视图,但在添加表窗口中它显示 InPatient 而不是 InPatients 并且它返回 null 值)

【问题讨论】:

  • 你为什么不写一个Linq 查询来做到这一点......你在使用entiry framework 吗?让我知道,以便我可以帮助您做到这一点。
  • 我正在使用实体框架核心@MdFaridUddinKiron。如何(在哪里)在类中编写查询。
  • 我希望创建一个 Custom View Model 与您的 PatientInPatient 的组合。在控制器中我会写Linq 将两个模型绑定到Custom View Model 而不是ApplicationDbContext 类中。

标签: c# entity-framework-core ef-code-first asp.net-core-webapi .net-5


【解决方案1】:

您可以在 Linq 表达式中加入两个模型并返回 ViewInPatient 列表:


var ViewInPatient_set =
    YourContext
    .InPatients
    .Select(i=> new ViewInPatient() 
        {
            Name = i.Patient.Name,
            // ...
            RoomNo = i.RoomNo,
            // ...
        }
    )
    .ToList();  // <-- transform to list is optional

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2013-10-09
    • 2014-01-26
    • 1970-01-01
    相关资源
    最近更新 更多