简体   繁体   中英

C# PostgreSQL Navigation Property Returns NULL

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

[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*

Lazy loading

The simplest way to use lazy-loading is by installing the Microsoft.EntityFrameworkCore.Proxies package and enabling it with a call to UseLazyLoadingProxies.

For example:

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

EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post.Blog and Blog.Posts navigation properties will be lazy-loaded.

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; }
}

Source: https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM