简体   繁体   English

导航属性未延迟加载EF6代码优先

[英]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.

相关问题 EF6不会延迟加载导航属性 - EF6 does not lazy load navigation property EF6延迟加载不适用于导航属性 - EF6 Lazy Loading not working for a navigation property 带有自定义关联的EF6代码优先导航属性 - EF6 Code first navigation property with custom association 在使用EF6和MVC 5的Code First迁移期间,navigation属性不是类型的声明属性 - The navigation property is not a declared property on type during Code First migration with EF6 and MVC 5 在EF6 C#中订购急切加载的导航属性数据 - Order Eager Loaded Navigation Property Data In EF6 C# 具有相同类型导航属性集合的EF6代码第一实体-如何告诉EF是什么关系? - EF6 Code First Entity with navigation property collection of same type - how do I tell EF what the relationship is? EF6代码优先-自引用配置导航属性 - EF6 Code First - Self-referencing configure navigation properties EF6代码从一到零,或者具有预定义的FK属性的一个 - EF6 code first one to zero or one with Predefined FK property 使用EF6代码优先,引用相同的属性名称 - Using EF6 code-first, reference same property name 在EF6代码中,首先我可以更改导航属性值,然后断开关系,并将两个更改都保存在一个事务中吗? - In EF6 Code First can I change a navigation property value, then break the relationship, and have both changes saved in one transaction?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM