[英]Need help on table design with Entity Framework Core
我正在開發一個簡單的 web 應用程序,其中醫生正在為患者添加多個處方記錄,並在開處方時將 select 多種葯物。 所以一個病人有多個處方,一個處方有多個選定的葯物。 為了報告目的/規范化角度,我使用了另一個表患者記錄,其中我引用了患者 ID 和處方 ID。
下面是患者、處方和葯物的 model, PatientRecord
表。
在運行遷移時,我收到此錯誤:
錯誤號:1769,State:1,Class:16
外鍵“FK_Drugs_Prescriptions_PrescriptionID”在引用表“Drugs”中引用了無效列“PrescriptionID”。
我對微軟網站上一對多關系的解釋感到困惑。
任何人都可以幫我嗎?
這里有些東西看起來不太對勁。 也許如果您清理它們,您將接近發現錯誤所在。
首先,我對您的PatientRecord
class 有點困惑。 它用PatientRecordId
標識自己,並映射到Patient
,但它不添加任何其他信息,那么它有什么用呢? 如果您不打算在 class 中添加任何內容,我認為您可以將其從 model 中刪除。
其次,您的Prescription
class 映射到Drugs
的集合。 這很完美,因為它們之間存在一對多關系……那么為什么它還具有 integer DrugId
屬性? 除非您希望Prescription
class 引用單個Drug
的Id
以及Drugs
的集合,否則我認為您應該將其刪除。 它可能會混淆實體框架並且沒有給您任何價值。
第三,您的Drug
class 映射到一個Prescription
(通過其屬性Prescription
和PrescriptionId
),但為什么呢? 據推測,一種葯物可以出現在多個處方上,因為它可以開給很多人,或者多次開給同一個人。 所以我認為你也想刪除它並用多對多關系替換它。
最后,如果你想在Prescription
和Drug
之間建立多對多關系(我想你會的),你可能需要添加DrugPrescription
class,具有Drug
屬性和Prescription
屬性,以創建這個多對-許多映射。
我認為如果您這樣做,您將非常接近您的目標,並且您的錯誤消息可能會 go 消失。
有兩種方法可以在 EF Core 中配置關系
約定:默認情況下,當在類型上發現導航屬性時,將創建關系。 不適用於多對多關系
Fluent API:您首先確定構成關系的導航屬性。 HasOne
或HasMany
標識您開始配置的實體類型的導航屬性。 HasOne/WithOne
用於參考導航屬性, HasMany/WithMany
用於集合導航屬性。
根據您的屏幕截圖和本傑明建議,您可以配置 model,如下所示
患者 - 處方 --> 一對多關系 處方 - 葯物 --> 多對多關系
public class Prescription
{
public int PrescriptionId { get; set; }
[Required]
public string Description { get; set; }
[Required]
public DateTime PrescriptionDate { get; set; }
public int PatientId { get; set; }
public Patient Patient { get; set; }
public ICollection<DrugPrescription> DrugPrescriptions { get; set; }
}
public class Drug
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int CurrentStock { get; set; }
public int DrugCost { get; set; }
public string Description { get; set; }
public ICollection<DrugPrescription> DrugPrescriptions { get; set; }
}
//represent a many-to-many relationship by including an entity class for
//the join table and mapping two separate one-to-many relationships.
public class DrugPrescription
{
public int DrugId { get; set; }
public Drug Drug { get; set; }
public int PrescriptionId { get; set; }
public Prescription Prescription { get; set; }
}
//DbContext
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{}
public DbSet<Patient> Patient { get;set; }
public DbSet<Drug> Drug { get;set; }
public DbSet<Prescription> Prescription { get;set; }
public DbSet<PatientRecord> PatientRecord { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
#region Drug-Prescription Many-to-Many
builder.Entity<DrugPrescription>()
.HasKey(dp => new { dp.DrugId, dp.PrescriptionId });
builder.Entity<DrugPrescription>()
.HasOne(dp => dp.Prescription)
.WithMany(p => p.DrugPrescriptions)
.HasForeignKey(dp => dp.PrescriptionId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<DrugPrescription>()
.HasOne(dp => dp.Drug)
.WithMany(d => d.DrugPrescriptions)
.HasForeignKey(dp => dp.DrugId)
.OnDelete(DeleteBehavior.Restrict);
#endregion
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.