public async Task Consume(ConsumeContext <StudentCreatedEvent> context) { var dbContext = lazyDbContext.Value; var user = await dbContext.Users.Include(x => x.UserRoles) .SingleOrDefaultAsync(x => x.Id == context.Message.UserId); if (user == null) { user = new HomeAssignmentsUser { Id = context.Message.UserId, Name = context.Message.Name, UserRoles = new List <HomeAssignmentsUserRole>() }; dbContext.Users.Add(user); } if (user.UserRoles.All(x => x.Id != context.Message.Id)) { dbContext.Students.Add(new Student { Id = context.Message.Id, User = user }); } await dbContext.SaveChangesAsync(); }
public async Task Consume(ConsumeContext <UserModifiedEvent> context) { var dbContext = lazyDbContext.Value; var user = await dbContext.Users.FindAsync(context.Message.Id); if (user == null) { user = new HomeAssignmentsUser { Id = context.Message.Id }; dbContext.Users.Add(user); } user.Name = context.Message.Name; await dbContext.SaveChangesAsync(); }