简体   繁体   中英

Why does Redis C# client method .getById() return null?

I'm building a simple blog application in Asp.Net MVC and I want to use a Redis database.

I have created a repository for my Users class that contains a few methods (create, getbyid and update). The probelm is the GetById(long id) method.

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;
        }
    }
}

Why does this method returnUser = redisUsers.GetById(id); always return null? I've been trying to find an answer on StackOverflow and other places but I couldn't.

The GetById() API (and all other ServiceStack.Redis Typed Client APIs) expect the POCOs to have a unique Id in an Id property.

So you should change your User model to use Id instead of UserId .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM