Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicating child array of entities during update #383

Closed
htmlpk opened this issue May 31, 2023 · 1 comment · Fixed by #498
Closed

Duplicating child array of entities during update #383

htmlpk opened this issue May 31, 2023 · 1 comment · Fixed by #498

Comments

@htmlpk
Copy link

htmlpk commented May 31, 2023

Versions:
.Net 7
Redis OM 0.5.2

Model:

 [Document(StorageType = StorageType.Json, Prefixes = new[] { "Lobby" })]
    public class Lobby : BaseGuidEntity
    {
        [Indexed]
        public GameType GameType { get; set; }

      [Indexed(CascadeDepth = 1)]
        public List<Player> Players { get; set; } = new List<Player>();
    }
public class BaseGuidEntity : IEntity<Guid>
   {
       [RedisIdField]
       [Indexed]
       public Guid Id { get; set; }
       [Indexed]
       public DateTime CreationDate { get; set; }

       public BaseGuidEntity()
       {
           Id = Guid.NewGuid();
           CreationDate = DateTime.Now;
       }
   }
public class Player:BaseGuidEntity
   {
       [Indexed]
       public long UserId { get; set; }

       [Indexed]
       public string? UserName { get; set; }

       [Indexed]
       public string? ConnectionId { get; set; }   
       
       [Indexed]
       public int Order { get; set; }
   }

Steps to reproduce:

  1. Create the model of lobby with 1 player
  2. Add 1 more player to the Players List and update model with UpdateAsync
public async Task<Lobby> Join(JoinLobbyViewModel model)
        {
            Lobby lobby = (await _lobbyRepository.GetByAsync(x => x.Id.Equals(Guid.Parse(model.GameId)))).FirstOrDefault();
            if (lobby == null)
            {
                throw new Exception("Game is not found");
            }
            Player player = new Player() { ConnectionId = model.ConnectionId, UserId = model.UserId, Order = lobby.Players.Count - 1, UserName = model.UserName };
            
            lobby.Players.Add(player);
            await _lobbyRepository.UpdateAsync(lobby);
            return lobby;
        }

Actual result:
3 Players presented in the Players List. After any update Player list duplicating himself with adding entity additional.
Expected result:
2 Players presented in Players List. After each update Player list not duplicating himself and adding new single entity.

It works fine with
_repo = (RedisCollection)provider.RedisCollection(false)
instead of
_repo =(RedisCollection)provider.RedisCollection()

@slorello89
Copy link
Member

Hi @htmlpk - looks like the issue here is that when Redis OM is building the diff, it is not accounting for the fact that the CreationDate is converted to a number in Redis (hence it's adding the extra record). Work around for now would be to set saveState to false when initializing _lobbyRepository, that will prevent it from preforming the diff.

@slorello89 slorello89 linked a pull request Oct 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants