public override void CreateRole(string roleName) { var newRole = new Role() {Name = roleName}; using (var context = new DatabaseContext()) { context.Roles.Add(newRole); context.SaveChanges(); } }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { // получаем логин пользователя var username = (string)context["UserName"]; if (string.IsNullOrEmpty(username) || collection.Count < 1) return; var db = new DatabaseContext(); // получаем id пользователя из таблицы Users по логину var firstOrDefault = db.Users.FirstOrDefault(u => u.Email.Equals(username)); if (firstOrDefault != null) { int userId = firstOrDefault.Id; // по этому id извлекаем профиль из таблицы профилей Profile profile = db.Profiles.FirstOrDefault(u => u.UserId == userId); // если такой профиль уже есть изменяем его if (profile != null) { foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profile.LastUpdateDate = DateTime.Now; db.Entry(profile).State = EntityState.Modified; } else { // если нет, то создаем новый профиль и добавляем его profile = new Profile(); foreach (SettingsPropertyValue val in collection) { profile.GetType().GetProperty(val.Property.Name).SetValue(profile, val.PropertyValue); } profile.LastUpdateDate = DateTime.Now; profile.UserId = userId; db.Profiles.Add(profile); } } db.SaveChanges(); }