我對物體框架有以下問題:
無法確定“串列”型別的導航“Booking.Participants”表示的關系。手動配置關系,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此屬性。
這是我的兩門課:
public class Booking
{
[Key]
public int Id { get; set; }
[Required]
public Event? Event { get; set; }
public Invoice? Invoice { get; set; }
[Required]
public Participant? TravelApplicant { get; set; }
public List<Participant>? Participants { get; set; }
[Required]
public int NumberOfParticipants
{
get
{
if (Participants != null)
{
if (TravelApplicant != null)
{
if (TravelApplicant.IsTravelApplicantParticipant)
return Participants.Count 1;
else
return Participants.Count;
}
}
return 0;
}
set
{
if (Participants != null)
{
if (TravelApplicant != null)
{
if (TravelApplicant.IsTravelApplicantParticipant)
NumberOfParticipants = Participants.Count 1;
else
NumberOfParticipants = Participants.Count;
}
}
}
}
[Required]
public bool CoronaEntryCheckBox { get; set; }
[Required]
public bool AgbCheckBox { get; set; }
[Required]
public bool NewsletterCheckBox { get; set; }
}
public class Participant
{
[Key]
public int Id { get; set; }
[Required]
public Accommodation? Accommodation { get; set; }
[Required]
public Booking? Booking { get; set; }
[Required]
public Gender Gender { get; set; }
public string? Title { get; set; }
[Required]
public string? FirstName { get; set; }
[Required]
public string? LastName { get; set; }
[Required]
public DateTime? Birthday { get; set; }
[Required]
public string? Street { get; set; }
[Required]
public string? HouseNumber { get; set; }
public string? AdditionalAddress { get; set; }
[Required]
public string? ZipCode { get; set; }
[Required]
public string? City { get; set; }
[Required]
public string? Country { get; set; }
[Required]
public string? EMail { get; set; }
[Required]
public string? Phone { get; set; }
[Required]
public bool IsTravelApplicant { get; set; }
[Required]
public bool IsTravelApplicantParticipant { get; set; }
[Required]
public bool Vegetarian { get; set; }
public string? Note { get; set; }
}
我希望你們中的一個人知道解決方案,提前謝謝你:)
uj5u.com熱心網友回復:
您在配置模型時是如何定義關系的?
從外觀上看,您仍然需要讓 Entity Framework 知道關系是如何鏈接的。理想情況下,您將BookingId存盤 a Participant,如果您將其標記為Required不應為空。
[Required]
public BookingId int {get; set; }
public virtual Booking Booking { get; set; }
我懷疑您實際上希望它Booking像這樣在類中定義:
public virtual List<Participant> Participants { get; set; } = new List<Participant>();
然后在你的資料庫背景關系中,你會在你的OnModelCreating覆寫中有一些類似的東西:
modelBuilder.Entity<Booking>(entity =>
{
entity.HasMany(x => x.Participants)
});
modelBuilder.Entity<Participant>(entity =>
{
entity.HasOne(x => x.Booking)
.HasForeignKey(k => k.BookingId);
});
您可以在https://docs.microsoft.com/en-us/ef/core/modeling/查看完整檔案
uj5u.com熱心網友回復:
EF 自動鏈接關系不一定會解決關系,尤其是當它們加倍時。您看到的問題可能是因為 Booking 與 Particirant (booking.TravelApplicant) 和一對多 (booking.Participants) 之間存在多對一關系。
Mike 的回答應該讓您整理好告訴 EF 使用 Participant.BookingId 鏈接回以解決 booking.Participants,同時您還應該在 Booking 上配置 TravelApplicantId FK 之類的東西來為 TravelApplicant 提供服務。EF 的基于約定的方法嘗試根據導航物體的型別而不是屬性的名稱來查找/使用 FK。這會導致在依賴約定時多次參考似乎失敗的大多數問題。
我通常不推薦這樣的結構,因為雖然暗示 TravelApplicant 將成為 Participants 集合的一部分,但無法在資料級別強制執行此操作。例如,如果我有 PersonA、PersonB 和 PersonC 的參與者,其中 PersonA 是申請人,那么沒有什么能阻止我從參與者中洗掉 PersonA(讓他們作為申請人)或將申請人更改為從未添加到參與者中的 PersonD。您當然可以在應用程式級別驗證這一點,但在資料級別沒有可用的保護。
或者,您可以將其構建為擁有一個申請人,并且該集合是額外的參與者。這意味著當需要所有人時,您將始終將申請人附加到參與者,并且不會防止將申請人添加到參與者關系中。
相反,我將使用的方法是使用多對多關系表 (BookingParticipants),使用諸如 SortOrder 或 IsApplicant 標志之類的東西來識別主申請人。這確保了主申請人始終是相關參與者。
使用主申請人是最低索引的排序順序,并且排序順序具有唯一約束,因此這可以滿足在資料庫級別保護關系和申請人。該標志更具可讀性,但缺乏在資料級別進行保護的能力,因為您可能將多個參與者標記為申請人,或者沒有參與者標記為申請人。“申請人”可以被物體公開為未映射的屬性,以獲取正確的參與者,但我建議在使用未映射的屬性時使用命名約定,以便于識別它們并確保它們不會最終出現在 EF Linq 中運算式。
uj5u.com熱心網友回復:
嘗試以代碼優先模式創建關系。
按照教程:[配置一對一-代碼優先][1]轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459617.html
上一篇:物體框架可以像下劃線一樣處理嗎?
