对象不包含定义?GetPosts,评论,评论,用户,帖子,SaveChanges,子评论,

编程入门 行业动态 更新时间:2024-10-27 22:25:41
本文介绍了对象不包含定义?GetPosts,评论,评论,用户,帖子,SaveChanges,子评论,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用Borker92.Models; 使用System.Data.Entity; 使用System.Linq; 使用System.Web.Mvc ; $ 命名空间Borker92.Controllers { 公共类GetPostController:控制器 { 私有对象dbContext; //获取:GetPost为 public ActionResult GetPosts() { IQueryable的< PostsVM>帖子= DbContext.GetPosts 。选择(p值=>新建PostsVM &NBSP ; { 帖子ID = p.PostID, 消息= p.Message, PostedDate = p.PostedDate.Value &NBSP ; })。AsQueryable(); 返回查看(帖子); } $ public PartialViewResult GetComments(int postId) { IQueryable的< CommentsVM> comments = dbContext.Comments.Where(c => c.Post.PostID == postId) 。选择(C =>新建CommentsVM &NBSP ; { ComID = c.ComID, CommentedDate = c.CommentedDate.Value, CommentMsg = c.CommentMsg, 用户=新UserVM { UserID = c.User.UserID, 用户名= c.User.Username, imageProfile = c.User.imageProfile } })。AsQueryable(); 返回PartialView(&〜/ Views / Shared / MyComments.cshtml",comments); } [HttpPost] public ActionResult AddComment(CommentsVM comment,int postId) { // bool result = false; 评论commentEntity = null; int userId =(int)Session [" UserID"]; var user = dbContext.Users.FirstOrDefault(u => u.UserID == userId); var post = dbContext.Posts.FirstOrDefault(p => p.PostID == postId); if(评论!= null) { $ commentEntity = new EDMX.Comment { CommentMsg = comment.CommentMsg, CommentedDate = comment.CommentedDate, } if(user!= null&& post!= null) { post.Comments.Add(commentEntity); user.Comments.Add(commentEntity); dbContext.SaveChanges(); // result = true; }¥b $ b } return RedirectToAction(" GetComments"," Comments",new {postId = postId}); }¥b $ b [HttpGet] public PartialViewResult GetSubComments(int ComID) { IQueryable的< SubCommentsVM> subComments = dbContext.SubComments.Where(sc => sc.Comment.ComID == ComID) 。选择(SC =>新建SubCommentsVM &NBSP ; { SubComID = sc.SubComID, CommentMsg = sc.CommentMsg, CommentedDate = sc.CommentedDate.Value, 用户=新UserVM & NBSP; { UserID = sc.User.UserID, 用户名= sc.User.Username, imageProfile = sc.User.imageProfile } })。AsQueryable(); 返回PartialView(&〜/ Views / Shared / MySubComments.cshtml",subComments); }¥b $ b } } 解决方案 您引用dbContext.SubComments但在的DbContext你的文件的顶部被定义为对象。对象没有任何这些成员,因此错误。您的dbContext变量需要定义为您定义的任何(DbContext派生)类型。这看起来像EF代码,因此它将是您创建的EF DbContext,然后添加您的各种DbSet< ...>属性为。

using Borker92.Models; using System.Data.Entity; using System.Linq; using System.Web.Mvc; namespace Borker92.Controllers { public class GetPostController : Controller { private object dbContext; // GET: GetPost public ActionResult GetPosts() { IQueryable<PostsVM> Posts = DbContext.GetPosts .Select(p => new PostsVM { PostID = p.PostID, Message = p.Message, PostedDate = p.PostedDate.Value }).AsQueryable(); return View(Posts); } public PartialViewResult GetComments(int postId) { IQueryable<CommentsVM> comments = dbContext.Comments.Where(c => c.Post.PostID == postId) .Select(c => new CommentsVM { ComID = c.ComID, CommentedDate = c.CommentedDate.Value, CommentMsg = c.CommentMsg, Users = new UserVM { UserID = c.User.UserID, Username = c.User.Username, imageProfile = c.User.imageProfile } }).AsQueryable(); return PartialView("~/Views/Shared/MyComments.cshtml", comments); } [HttpPost] public ActionResult AddComment(CommentsVM comment, int postId) { //bool result = false; Comment commentEntity = null; int userId = (int)Session["UserID"]; var user = dbContext.Users.FirstOrDefault(u => u.UserID == userId); var post = dbContext.Posts.FirstOrDefault(p => p.PostID == postId); if (comment != null) { commentEntity = new EDMX.Comment { CommentMsg = comment.CommentMsg, CommentedDate = comment.CommentedDate, }; if (user != null && post != null) { post.Comments.Add(commentEntity); user.Comments.Add(commentEntity); dbContext.SaveChanges(); //result = true; } } return RedirectToAction("GetComments", "Comments", new { postId = postId }); } [HttpGet] public PartialViewResult GetSubComments(int ComID) { IQueryable<SubCommentsVM> subComments = dbContext.SubComments.Where(sc => sc.Comment.ComID == ComID) .Select(sc => new SubCommentsVM { SubComID = sc.SubComID, CommentMsg = sc.CommentMsg, CommentedDate = sc.CommentedDate.Value, User = new UserVM { UserID = sc.User.UserID, Username = sc.User.Username, imageProfile = sc.User.imageProfile } }).AsQueryable(); return PartialView("~/Views/Shared/MySubComments.cshtml", subComments); } } }

解决方案

You are referencing dbContext.SubComments but dbContext at the top of your file is defined to be object. Object doesn't have any of these members, hence the error. Your dbContext variable needs to be defined as whatever (DbContext-derived) type you have defined. This looks like EF code so it'll be the EF DbContext you created and then added your various DbSet<…> properties to.

更多推荐

对象不包含定义?GetPosts,评论,评论,用户,帖子,SaveChanges,子评论,

本文发布于:2023-10-23 08:22:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包含   定义   对象   帖子   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!