【问题标题】:C# PostgreSQL Navigation Property Returns NULLC# PostgreSQL 导航属性返回 NULL
【发布时间】:2022-01-04 22:27:38
【问题描述】:

我无法获得 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*

【问题讨论】:

    标签: c# .net postgresql entity-framework entity-framework-core


    【解决方案1】:

    延迟加载

    使用延迟加载的最简单方法是安装 Microsoft.EntityFrameworkCore.Proxies 包并通过调用 UseLazyLoadingProxies 来启用它。

    例如:

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

    EF Core 将为任何可以被覆盖的导航属性启用延迟加载 - 也就是说,它必须是 virtual 以及可以继承的类。例如,在以下实体中,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

    【讨论】:

    • {"$id":"1","data":{"$id":"2","$values":[{"$id":"3","country ":{"$id":"4","name":"Turkey","zipCode":"123","id":2,"createDate":"2021-12-02T21:15:53.547457", "updateDate":null,"active":true},"name":"İstanbul","zipCode":"34000","id":1,"createDate":"2021-12-02T22:27:20.462915" ,"updateDate":null,"active":true},{"$id":"5","country":{"$id":"6","name":null,"zipCode":null, "id":0,"createDate":"0001-01-01T00:00:00","updateDate":null,"active":false},"name":"Istanbul","zipCode":"34" ,"id":2,"createDate":"2022-01-02T01:23:29.691223","updateDate":null,"active":true}... 只是第一个相关数据返回
    猜你喜欢
    • 2018-11-07
    • 2012-11-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2023-01-20
    相关资源
    最近更新 更多