I running the Windows server version of Redis and then have a little client application, and I cant find out how I update a value in a datatype I already have in the database
I have this Leader class I would like to update if it exists for a given District, else create an new entry
public class Leader
{
public Employee Employee { get; set; }
public District District { get; set; }
}
public void SetLeader(District district, Employee employee)
{
using (var leaderSession = _redisClient.GetTypedClient<Leader>())
{
var leader = leaderSession.GetAll().SingleOrDefault(x => x.District.Id == district.Id);
if (leader != null)
{
leader.Employee = employee;
}
else
{
leader = new Leader{District = district, Employee = employee};
}
leaderSession.Store(leader);
}
}
But when I do this I just get an extra entry in the database, so Store is not the right command to use, but which should I use?
The only solution I can come up with is delete the old one and store a new one, but thats seems a bit ineffective? (Not that it matters in my example, but still)
Edit: doing delete dont work either, it still creates a new one and keeps the old one.
public void SetLeader(District district, Employee employee)
{
using (var leaderSession = _redisClient.GetTypedClient<Leader>())
{
var leader = leaderSession.GetAll().SingleOrDefault(x => x.District.Id == district.Id);
if (leader != null)
{
leaderSession.Delete(leader);
}
leader = new Leader { District = district, Employee = employee };
leaderSession.Store(leader);
}
}
Edit 2 Adding a Id field to Leader was apparently necessary for it to distinguish the different rows
So changing it to this works
public class Leader
{
public long Id { get; set; }
public Employee Employee { get; set; }
public District District { get; set; }
}
Adding a Id field to Leader was apparently necessary for it to distinguish the different rows
So changing it to this works