[英]Navigation Property not lazy loaded EF6 Code First
I've scoured the web and stack overflow for an answer for my problem but I've got no luck. 我已经在网上搜寻了堆栈,并为我的问题提供了堆栈溢出的答案,但是我没有运气。
Basically I have a model that has 2 references of the same object and other objects as well. 基本上,我有一个模型,该模型具有相同对象和其他对象的2个引用。 When I insert a record to the model, the properties are not loaded hence I tried the Context.Attach function to no luck. 当我向模型中插入一条记录时,不会加载属性,因此我尝试使用Context.Attach函数没有任何运气。 I also tried querying an existing record and the object still does not attach 我也尝试查询现有记录,但该对象仍未附加
Here is my Model 这是我的模特
[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; }
Config for the single object multiple reference 配置单个对象的多个引用
modelBuilder.Entity<TripHistory>()
.HasRequired(th => th.Customer)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<TripHistory>()
.HasOptional(th => th.Driver)
.WithMany()
.WillCascadeOnDelete(false);
Any help would be greatly appreciated 任何帮助将不胜感激
For future reference, 备查,
For some reason I had to force the loading of the objects even though lazy loading is enabled by using the .Include() method. 由于某些原因,即使使用.Include()方法启用了延迟加载,我也必须强制加载对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.