C# 刚在模型中创建的记录有空的相关表,而不是空的,这会破坏我的查询吗?
我有一个论坛,通过以下操作发布回复:C# 刚在模型中创建的记录有空的相关表,而不是空的,这会破坏我的查询吗?,c#,asp.net-mvc,entity-framework,linq,entity-framework-6,C#,Asp.net Mvc,Entity Framework,Linq,Entity Framework 6,我有一个论坛,通过以下操作发布回复: var newreply = new ForumReply(); newreply.ForumPostID = ForumPostID; newreply.DateCreated = DateTime.Now; newreply.Reply = Reply; newreply.UserID = currentuser; db.ForumReply.Add(newreply);
var newreply = new ForumReply();
newreply.ForumPostID = ForumPostID;
newreply.DateCreated = DateTime.Now;
newreply.Reply = Reply;
newreply.UserID = currentuser;
db.ForumReply.Add(newreply);
db.SaveChanges();
var viewModel = GetPosts(ForumTopicID);
return PartialView("_forumposts", viewModel);
GetPosts操作如下所示:
string currentuser = User.Identity.GetUserId();
var topic = db.ForumTopic
.Where(i => i.ForumTopicID == ForumTopicID && i.Forum.Cohort.enrollment.Any(h => h.UserID == currentuser))
.Include(s => s.Forum)
.Include(g => g.Forum.Cohort)
.Include(f => f.Forum.Cohort.User)
.Include(w => w.ForumPost.Select(g => g.ForumReply))
.Single();
var viewModel = new PostMainViewModel();
viewModel.Cohortname = topic.Forum.Cohort.Cohortname;
viewModel.Tutorname = topic.Forum.Cohort.User.Fname + " " + topic.Forum.Cohort.User.Lname;
viewModel.ForumID = topic.Forum.ForumID;
viewModel.ForumTitle = topic.Forum.ForumTitle;
viewModel.ForumTopicID = topic.ForumTopicID;
viewModel.TopicTitle = topic.TopicTitle;
viewModel.TopicDescription = topic.TopicDescription;
var posts = topic.ForumPost
.OrderBy(h => h.DateCreated)
.Select(x => new PostViewModel
{
ForumPostID = x.ForumPostID,
Post = x.Post,
DateCreated = x.DateCreated,
UserName = x.User.Fname + " " + x.User.Lname,
Likes = x.ForumPostLike.Count(),
Isliked = x.ForumPostLike.Any(f => f.UserID == currentuser),
Usermade = x.UserID == currentuser,
ProfileImage = x.User.personal?.ProfileImage,
ReplyViewModel = x.ForumReply
.Select(y => new ReplyViewModel
{
ForumReplyID = y.ForumReplyID,
Reply = y.Reply,
DateCreated = y.DateCreated,
UserName = y.User.Fname + " " + y.User.Lname,
Likes = y.ForumReplyLike.Count(),
Isliked = y.ForumReplyLike.Any(f => f.UserID == currentuser),
Usermade = y.UserID == currentuser,
ProfileImage = y.User.personal?.ProfileImage,
})
});
viewModel.PostViewModel = posts;
return viewModel;
GetPosts操作用于加载页面上的帖子/回复,工作正常,但是当它按照上述操作在回复之后加载帖子/回复时,我在ForumReplies的ForumReplyLike表上得到一个空值错误。这是因为,当我创建“topics”对象而不是查询表fresh时,它似乎将我刚刚创建的ForumReply对象携带到topics对象中,这意味着ForumReplyLike在查询中已经是null而不是空的。这是我对它的理解,无论如何,它很可能是不正确的
我想知道的是为什么会发生这种情况,以及如何解决它,但仍然使用相同的查询。我知道我可以通过将GetPosts操作中的查询方式更改为几个foreach循环来解决实际问题。不过,我想继续使用相同的查询操作,这都是直接转换为SQL的操作,这意味着我无法使用c#处理查询中的null
如果有人能比我更好地理解这些事情,并能准确地解释这里发生了什么,以及我如何阻止topics对象受到刚刚发布的forumreply的影响,我将不胜感激
编辑-我可以看出,我可以通过处理dbcontext然后初始化一个新的dbcontext来解决这个问题,但可能有更好的方法。您只需将
ForumPostLike
属性初始化为空集合即可。我猜您当前的代码是这样的:
public virtual ICollection<Something> ForumPostLike { get; set; }
这只适用于C#6。虽然OP可能使用的是C#6,但问题被标记为EF6,因此他们更有可能仍然使用C#5。您还应该发布确保默认值的旧方法。@ChrisPratt问题中发布的代码包含空传播运算符,因此使用了C#6。不管怎样,我的回答中描述了使用早期版本的方法(“您可以在构造函数中将其初始化为空列表”)。
public virtual ICollection<Something> ForumPostLike { get; set; } = new List<Something>();