简体   繁体   English

C# PostgreSQL 导航属性返回 NULL

[英]C# PostgreSQL Navigation Property Returns NULL

I couldn't get Navigation Prop. 1)Can I make it without Include().我无法获得 Navigation Prop. 1) 我可以在没有 Include() 的情况下完成它。 2) How? 2)如何? It seems there is a few way.似乎有几种方法。 Which is the easiest哪个最简单

I couldn't get Navigation Prop. 1)Can I make it without Include().我无法获得 Navigation Prop. 1) 我可以在没有 Include() 的情况下完成它。 2) How? 2)如何? It seems there is a few way.似乎有几种方法。 Which is the easiest哪个最简单

I couldn't get Navigation Prop. 1)Can I make it without Include().我无法获得 Navigation Prop. 1) 我可以在没有 Include() 的情况下完成它。 2) How? 2)如何? It seems there is a few way.似乎有几种方法。 Which is the easiest哪个最简单

I couldn't get Navigation Prop. 1)Can I make it without Include().我无法获得 Navigation Prop. 1) 我可以在没有 Include() 的情况下完成它。 2) How? 2)如何? 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.使用延迟加载的最简单方法是安装Microsoft.EntityFrameworkCore.Proxies package 并通过调用 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.然后,EF Core 将为任何可以被覆盖的导航属性启用延迟加载——也就是说,它必须是virtual的并且在可以继承的 class 上。 For example, in the following entities, the Post.Blog and Blog.Posts navigation properties will be lazy-loaded.例如,在以下实体中,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; }
}

Source: https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy资料来源: 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