簡體   English   中英

與實體框架相關的實體的加載無需顯式的“加載”或“包含”調用

[英]Entity Framework related entities are loaded without explicit Load or Include call

我有一個生成的實體模型,如下所示:

public partial class Entity
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Entity()
    {
        this.Comments = new HashSet<Comment>();
        this.Fields = new HashSet<FieldEntity>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public int AssigneeID { get; set; }
    public string Description { get; set; }
    public System.DateTime Created { get; set; }
    public int CreatedBy { get; set; }
    public System.DateTime LastEdited { get; set; }
    public int LastEditBy { get; set; }
    public bool Deleted { get; set; }
    public Nullable<System.DateTime> DeletedDate { get; set; }

    public virtual UserEntity Assignee { get; set; }
    public virtual UserEntity CreatedByUser { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Comment> Comments { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<FieldEntity> Fields { get; set; }
    public virtual UserEntity LastEditByUser { get; set; }
}

當我使用EF LinqToSql提取此實體時,由於某種原因,即使我不使用此處指定的Include()Load() ,也會填充CommentsFields列表。

這是我使用的示例查詢:

var entities = ctx.MyEntities.Single(x => x.ID == 1);

我認為默認行為是延遲加載相關實體,因此我試圖確定查詢或模型是否存在問題。

對於它的價值,我確實在我的解決方案中搜索了LazyLoadingEnabled ,並且應該將其啟用:

//from MyDBModel.edmx
<EntityContainer Name="MyEntities" annotation:LazyLoadingEnabled="true">
...

默認情況下,在EF中啟用了延遲加載。 如果要禁用它,則可以檢查與同一主題相關的另一個問題的答案。

另外,您可以使用SQL Server Profiler工具確認代碼是否正在執行延遲加載。 檢查此鏈接以獲取有關如何執行此操作的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM