【问题标题】:Asp.net core navigation property always nullAsp.net 核心导航属性始终为空
【发布时间】:2021-10-07 09:37:50
【问题描述】:

当我尝试访问 Comment 类的 Author 属性时,它始终为 null。我尝试在查询中使用 Include() 并且还安装了延迟加载代理,但无济于事。我正在使用带有 .NET 5 的最新版本的 Asp.net 核心。 这是我的评论类:

public class Comment {
    public virtual int Id { get; set; }

    public virtual int PostReplyToId { get; set; }
    public virtual Post PostReplyTo { get; set; }

    public int? ReplyToId { get; set; }
    public virtual Comment ReplyTo { get; set; }

    public virtual ICollection<Comment> Replies { get; set; }

    public virtual string AuthorId { get; set; }
    public virtual ApplicationUser Author { get; set; }

    public virtual DateTime TimePosted { get; set; }

    public virtual string Content { get; set; }

    public virtual bool Visible { get; set; }

    [NotMapped]
    public string CommentInvalid { get; set; }
}

这是我的 ApplicationUser 类:

public class ApplicationUser : IdentityUser {
    [ForeignKey("AuthorId")]
    public virtual ICollection<Post> PostsAuthored { get; set; }

    [ForeignKey("AuthorId")]
    public virtual ICollection<Comment> CommentsAuthored { get; set; }
}

这是我访问评论的 Author 属性的视图部分(我在这里得到 NullReferenceException,因为 comment.Author 为空):

<div>
    <h4>Comments</h4>
    <p><a href="#" class="CommentReplyButton" data-commentId="null">Create a comment</a></p>
    @foreach (Comment comment in (List<Comment>)ViewBag.Comments) {
        if (comment.ReplyToId == null) {
            <div class="media" id="@("Comment" + comment.Id)">
                <a href="#"><img class="mr-3" src="~/images/pfp.png" alt="@comment.Author.Id's Profile Picture" /></a> <!-- NullReferenceException here -->

我用来设置 ViewBag.Comments 的查询是这样的(我不认为 Include 是必要的,因为没有它我也有同样的问题):

List<Comment> comments = (from comment in _context.Comment.Include(e => e.Author)
                          where comment.PostReplyToId == post.Id
                          select comment).ToList();

ViewBag.Comments = comments;

我运行了 Serge 的查询,同样的事情发生了。这是视觉工作室所说的评论等于(ViewBag.Comments 只是一个 1 元素长列表) Query Result 我知道评论绝对不是空的,因为我可以从上面的行中成功获取 Id。我检查了数据库,我知道在评论中正确设置了 AuthorId,并且评论的 AuthorId 字段设置正确。我知道它应该指向的 ApplicationUser 存在,因为它在数据库中,我可以使用它登录。 感谢大家的帮助。

【问题讨论】:

  • 您特别尝试了哪些查询?
  • @abdusco 我已经更新了我的问题以包含我在其中使用的查询。

标签: c# asp.net-mvc entity-framework-core asp.net-identity


【解决方案1】:

在 Comment 类中你不需要这么多的虚拟属性。并尝试修复作者的导航属性。它应该工作

public class Comment {
    [Key]
    public  int Id { get; set; }

    public  int PostReplyToId { get; set; }
    public virtual Post PostReplyTo { get; set; }

    public int? ReplyToId { get; set; }
    public virtual Comment ReplyTo { get; set; }

    public virtual ICollection<Comment> Replies { get; set; }

    public  string  AuthorId { get; set; }
    [ForeignKey(nameof(AuthorId ))]
    [InverseProperty(nameof(ApplicationUser.CommentsAuthored))]
    public virtual ApplicationUser Author { get; set; }

    public DateTime TimePosted { get; set; }

    public  string Content { get; set; }

    public bool Visible { get; set; }

    [NotMapped]
    public string CommentInvalid { get; set; }
}

public class ApplicationUser : IdentityUser {

   .....

   [InverseProperty(nameof(Comment.Author))]
    public virtual ICollection<Comment> CommentsAuthored { get; set; }
}

并修复您的查询

var comments= _context.Comment.Include(e => e.Author)
                          .Where ( c=> c.PostReplyToId == post.Id)
                          .ToList();

或者只是为了调试试试这个

var comments = (from c in _context.Comment
                 join a in  context.Author on c.AuthorId equals a.Id
                  where c.PostReplyToId == post.Id
                  select new { comments=c, Author=a}).ToList();

【讨论】:

  • 这行不通。您必须添加导航属性并进行数据库迁移。只有在此之后。
  • 添加导航属性是什么意思?我使用了您的代码并进行了数据库迁移,但上下都是空的。我已将查询更新为您建议的查询,但行为没有变化。
  • 您确定在 c.PostReplyToId == post.Id 的记录中没有空字符串 AuthorId?有些东西告诉我 AuthorId 是整数或长
  • 感谢您的快速回复。感谢您抽出宝贵时间帮助我。我的测试网站上只有 1 条评论和 1 条用户。我已经检查了 SSMS,并且我的评论的作者 ID 肯定与我的用户在用户表中的条目相匹配。如果您认为有帮助,我可以将数据库条目放入问题中。
  • 我的问题是您确定 IdentityUser Id 是字符串类型吗?
【解决方案2】:

我删除了整个数据库并重新迁移了所有内容并修复了它。我想我以某种方式损坏了我的数据库。我从服务器上删除了整个数据库,删除了所有迁移,然后创建了一个新的并更新了数据库。它不是最佳的,因为我丢失了所有数据,但它现在可以工作了。感谢@Serge 的帮助。

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多