为什么数据库不更改

时间:2019-08-07 09:48:00

标签: asp.net asp.net-mvc

我想知道为什么数据库不修改用户值和帖子值。我在ASP.NET Core中写了同样的东西,数据库修改了用户的声誉

        public void AddRatingPost(string userId, int postId)
        {
            var item = db.Ratings.SingleOrDefault(x => x.UserId == userId && x.PostId == postId);
            var postUserId = postService.GetPostById(postId).UserId;

            Rating rating = new Rating
            {
                UserId = userId,
                PostId = postId
            };
            if (item == null)
            {
                var user = userService.GetUserById(postUserId).Reputation++;
                var post = postService.GetPostById(postId).Reputation++;
                db.Ratings.Add(rating);

            }
            else
            {
                var user = userService.GetUserById(postUserId).Reputation--;
                var post = postService.GetPostById(postId).Reputation--;
                db.Ratings.Remove(item);

            }

            db.SaveChanges();
        }

这是UserService

    public class UserService : IUserService
    {
        private ApplicationDbContext db;

        public UserService(ApplicationDbContext _db)
        {
            db = _db;
        }
        public ApplicationUser GetUserById(string userId)
        {
           var item =  db.Users.SingleOrDefault(x => x.Id == userId);
           return item;
        }
    }

0 个答案:

没有答案