[英]Navigation Property not lazy loaded EF6 Code First
我已經在網上搜尋了堆棧,並為我的問題提供了堆棧溢出的答案,但是我沒有運氣。
基本上,我有一個模型,該模型具有相同對象和其他對象的2個引用。 當我向模型中插入一條記錄時,不會加載屬性,因此我嘗試使用Context.Attach函數沒有任何運氣。 我也嘗試查詢現有記錄,但該對象仍未附加
這是我的模特
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid TripID { get; set; }
[Required]
[Range(1, 999999, ErrorMessage = "Total Distance can only be a positive number non-zero")]
public double TotalDistance { get; set; }
[Required]
public double AmountDue { get; set; }
[Required]
public TripPaymentType PaymentType { get; set; }
[Required]
public TripStatus CurrentStatus { get; set; }
[ForeignKey("TripTypeId")]
public TripType TripType { get; set; }
[Required]
public Guid TripTypeId { get; set; }
[Required]
public DateTime TransactionDate { get; set; }
[Required]
public Guid CustomerID { get; set; }
[ForeignKey("CustomerID")]
public UserInfo Customer { get; set; }
public Guid? DriverID { get; set; }
[ForeignKey("DriverID")]
public UserInfo Driver { get; set; }
配置單個對象的多個引用
modelBuilder.Entity<TripHistory>()
.HasRequired(th => th.Customer)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<TripHistory>()
.HasOptional(th => th.Driver)
.WithMany()
.WillCascadeOnDelete(false);
任何幫助將不勝感激
備查,
由於某些原因,即使使用.Include()方法啟用了延遲加載,我也必須強制加載對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.