簡體   English   中英

C# PostgreSQL 導航屬性返回 NULL

[英]C# PostgreSQL Navigation Property Returns NULL

我無法獲得 Navigation Prop. 1) 我可以在沒有 Include() 的情況下完成它。 2)如何? 似乎有幾種方法。 哪個最簡單

我無法獲得 Navigation Prop. 1) 我可以在沒有 Include() 的情況下完成它。 2)如何? 似乎有幾種方法。 哪個最簡單

我無法獲得 Navigation Prop. 1) 我可以在沒有 Include() 的情況下完成它。 2)如何? 似乎有幾種方法。 哪個最簡單

我無法獲得 Navigation Prop. 1) 我可以在沒有 Include() 的情況下完成它。 2)如何? 似乎有幾種方法。 哪個最簡單

[Table("schools")]
    public class School : BaseEntity<int>
    {
        public School()
        {
            Category  = new Category();
            District = new District();
        }
        [JsonIgnore]
        [Column("category_id")]
        [ForeignKey("Category")]
        public short CategoryId { get; set; }
        public Category Category { get; set; }

        [JsonIgnore]
        [Column("district_id")]
        [ForeignKey("District")]
        public int DistrictId { get; set; }
        public District District { get; set; }

        [Column("name")]
        public string Name { get; set; }

        [Column("rating")]
        public decimal Rating { get; set; }

        [Column("vote_count")]
        public int VoteCount { get; set; }

        [Column("comment_count")]
        public int CommentCount { get; set; }

        [JsonIgnore]
        public virtual IList<SchoolComment> SchoolComments { get; set; }
    }
/////////////
    [Table("comments")]
    public class Comment : BaseEntity<int>
    {
        public Comment()
        {
            Commenter = new Commenter();
        }

        [JsonIgnore]
        [Column("commenter_id")]
        [ForeignKey("Commenter")]
        public int CommenterId { get; set; }
        public Commenter Commenter { get; set; }
        [Column("text")]
        public string Text { get; set; }

        [Column("like_count")]
        public int LikeCount { get; set; }

        [Column("dislike_count")]
        public int DislikeCount { get; set; }

        [JsonIgnore]
        [InverseProperty("Comment")]
        public virtual IList<Reply> Replies { get; set; }

        [JsonIgnore]
        [InverseProperty("Comment")]
        public virtual IList<SchoolComment> SchoolComments { get; set; }
    }
////
    [Table("school_comments")]
    public class SchoolComment : BaseEntity<int>
    {
        public SchoolComment()
        {
            Comment = new Comment();
            School = new School();
        }
        [JsonIgnore]
        [Column("comment_id")]
        [ForeignKey("Comment")]
        public int CommentId { get; set; }
        public Comment Comment { get; set; }

        [JsonIgnore]
        [Column("school_id")]
        [ForeignKey("School")]
        public int SchoolId { get; set; }
        public School School { get; set; }

        [Column("rating")]
        public int Rating { get; set; }

        [JsonIgnore]
        [InverseProperty("SchoolComment")]
        public virtual IList<Reply> Replies { get; set; }
    }
////

        public override List<SchoolComment> GetList(Func<SchoolComment, bool> filter = null)
        {
            using Context context = new Context();
            return filter == null
                ? context.Set<SchoolComment>().Include(p => p.Comment).Include(p => p.School).ToList()
                : context.Set<SchoolComment>().Include(p => p.Comment).Include(p => p.School).Where(filter).ToList();
        }
```
*I couldn't get Navigation Prop. 1)Can I make it without Include(). 2) How? It seems there is a few way. Which is the easiest*

*I couldn't get Navigation Prop. 1)Can I make it without Include(). 2) How? It seems there is a few way. Which is the easiest*

*I couldn't get Navigation Prop. 1)Can I make it without Include(). 2) How? It seems there is a few way. Which is the easiest*

*I couldn't get Navigation Prop. 1)Can I make it without Include(). 2) How? It seems there is a few way. Which is the easiest*

延遲加載

使用延遲加載的最簡單方法是安裝Microsoft.EntityFrameworkCore.Proxies package 並通過調用 UseLazyLoadingProxies 啟用它。

例如:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLazyLoadingProxies()
        .UseNpgsql(myConnectionString);

然后,EF Core 將為任何可以被覆蓋的導航屬性啟用延遲加載——也就是說,它必須是virtual的並且在可以繼承的 class 上。 例如,在以下實體中,Post.Blog 和 Blog.Posts 導航屬性將被延遲加載。

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public virtual Blog Blog { get; set; }
}

資料來源: https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy

暫無
暫無

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

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