public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { // получаем логин пользователя var username = (string)context["UserName"]; if (string.IsNullOrEmpty(username) || collection.Count < 1) return; var user = userService.GetAllEntities().FirstOrDefault(u => u.Email.Equals(username)); if (user == null) return; var profile = user.Profile; // получаем id пользователя из таблицы Users по логину if (profile != null) { foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profile.LastUpdate = DateTime.Now; } else { // если нет, то создаем новый профиль и добавляем его profile = new ProfileEntity(); foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profile.LastUpdate = DateTime.Now; profile.Id = user.Id; profileService.Create(profile); } }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { // получаем логин пользователя var username = (string)context["UserName"]; if (string.IsNullOrEmpty(username) || collection.Count < 1) return; // получаем id пользователя из таблицы Users по логину var firstOrDefault = userService.GetUserEntityByEmail(username); if (firstOrDefault != null) { int userId = firstOrDefault.Id; // по этому id извлекаем профиль из таблицы профилей ProfileEntity profile = profileService.GetProfileByUserId(userId); // если такой профиль уже есть изменяем его if (profile != null) { foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profileService.UpdateProfile(profile); } else { // если нет, то создаем новый профиль и добавляем его profile = new ProfileEntity(); foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profileService.CreateProfile(profile); } } }