为什么Redis C#客户端方法.getById()返回null?

时间:2018-08-30 19:53:55

标签: c# asp.net redis servicestack repository-pattern

我正在Asp.Net MVC中构建一个简单的博客应用程序,并且我想使用Redis数据库。

我已经为我的Users类创建了一个存储库,其中包含一些方法(create,getbyid和update)。探针是 GetById(long id)方法。

using System;
using System.Collections.Generic;
using System.Text;
using ServiceStack.Redis;
using Domain.Entities;
using RedisDataLayer.AbstractRepository;
using System.Linq;

namespace RedisDataLayer.ConcreteRepository
{
    public class UserRepository : IUserRepository
    {
        private readonly IRedisClient _redisClient;

        public UserRepository(IRedisClient redisClient)
        {
            _redisClient = redisClient;
        }

        public User Create(User user)
        {
            var redisUsers = _redisClient.As<User>();

            user.UserId = redisUsers.GetNextSequence();
            redisUsers.Store(user);

            return user;
        }



        public User GetById(long id)
        {
            var redisUsers = _redisClient.As<User>();

            User returnUser;

            returnUser = redisUsers.GetById(id);

            return returnUser;
        }

        public User Update(User user)
        {
            var redisUsers = _redisClient.As<User>();

            User savedUser = redisUsers.GetById(user.UserId);


            savedUser.Username = user.Username;
            savedUser.Password = user.Password;
            savedUser.UserId = user.UserId;

            foreach (var blogId in user.BlogIds)
            {
                savedUser.BlogIds.Add(blogId);
            }

            redisUsers.Store(savedUser);

            return savedUser;
        }
    }
}

为什么此方法returnUser = redisUsers.GetById(id);总是返回null?我一直在尝试在StackOverflow和其他地方找到答案,但是我找不到。

1 个答案:

答案 0 :(得分:4)

.reloadAction( "restartBenefits", "Ok. Let's start over.", { matches: /^start over$|^restart$/i } ) .cancelAction( "cancelRequest", "Thank you for reaching out, Good bye!", { matches: /^nevermind$|^cancel$|^cancel.*request/i, confirmPrompt: "This will cancel your request. Are you sure?" } ); API(以及所有其他ServiceStack.Redis类型客户端API)期望POCO在GetById()属性中具有唯一的ID。

因此,您应该将用户模型更改为使用Id而不是Id