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 string[] GetRolesForUser(string email)
        {
            using (var context = new DatabaseContext())
            {
                var roles = new string[] {};
                var user = context.Users.FirstOrDefault(u => u.Email == email);

                if (user == null) return roles;

                var userRole = user.Role;

                if (userRole != null)
                {
                    roles = new string[] {userRole.Name};
                }
                return roles;
            }
        }
        public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
        {
            // коллекция, которая возвращает значения свойств профиля
            var result = new SettingsPropertyValueCollection();

            if (collection == null || collection.Count < 1 || context == null)
            {
                return result;
            }

            // получаем из контекста имя пользователя - логин в системе
            var username = (string)context["UserName"];
            if (String.IsNullOrEmpty(username)) return result;

            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 (SettingsProperty prop in collection)
                    {
                        var spv = new SettingsPropertyValue(prop)
                        {
                            PropertyValue = profile.GetType().GetProperty(prop.Name).GetValue(profile, null)
                        };
                        result.Add(spv);
                    }
                }
                else
                {
                    foreach (SettingsProperty prop in collection)
                    {
                        var svp = new SettingsPropertyValue(prop) {PropertyValue = null};
                        result.Add(svp);
                    }
                }
            }
            return result;
        }
        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();
        }