不能丢弃数据库"练习"因为它当前正在使用,当您尝试连接到数据库时

时间:2016-01-23 16:23:59

标签: c# asp.net asp.net-mvc asp.net-mvc-5

当您尝试执行某个操作时,例如:登录页面时出现错误:无法删除数据库"练习"因为它目前正在使用中。

问题是什么?

"抱歉我的英语不好"

提供商:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using CustomRoleProviderApp;
    using Models;
    using System.Web.Security;

    namespace CustomRoleProviderApp.Providers
    {
        public class CustomRoleProvider : RoleProvider
        {
            public override string[] GetRolesForUser(string username)
            {
                string[] role = new string[] { };
                using (PracticeDB db = new PracticeDB())
                {
                    // Получаем пользователя
                    Users user = db.Users.FirstOrDefault(u => u.Name == username);
                    if (user != null)
                    {
                        // получаем роль
                        Models.Roles userRole = db.Roles.Find(user.RoleID);
                        if (userRole != null)
                            role = new string[] { userRole.RoleName };
                    }
                }
                return role;
            }
            public override void CreateRole(string roleName)
            {
                Models.Roles newRole = new Models.Roles() { RoleName = roleName };
                PracticeDB db = new PracticeDB();
                db.Roles.Add(newRole);
                db.SaveChanges();
            }
            public override bool IsUserInRole(string username, string roleName)
            {
                bool outputResult = false;
                // Находим пользователя
                using (PracticeDB db = new PracticeDB())
                {
                    // Получаем пользователя
                    Users user = db.Users.FirstOrDefault(u => u.Name == username);
                    if (user != null)
                    {
                        // получаем роль
                        Models.Roles userRole = db.Roles.Find(user.RoleID);
                        //сравниваем
                        if (userRole != null && userRole.RoleName == roleName)
                            outputResult = true;
                    }
                }
                return outputResult;
            }
            public override void AddUsersToRoles(string[] usernames, string[] roleNames)
            {
                throw new NotImplementedException();
            }

            public override string ApplicationName
            {
                get { throw new NotImplementedException(); }
                set { throw new NotImplementedException(); }
            }

            public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
            {
                throw new NotImplementedException();
            }

            public override string[] FindUsersInRole(string roleName, string usernameToMatch)
            {
                throw new NotImplementedException();
            }

            public override string[] GetAllRoles()
            {
                throw new NotImplementedException();
            }

            public override string[] GetUsersInRole(string roleName)
            {
                throw new NotImplementedException();
            }

            public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
            {
                throw new NotImplementedException();
            }

            public override bool RoleExists(string roleName)
            {
                throw new NotImplementedException();
            }
        }
    }

模型角色:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    namespace Models
    {
        public partial class Roles
        {
            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
            public Roles()
            {
                this.Users = new HashSet<Users>();
            }

            public int RoleID { get; set; }
            public string RoleName { get; set; }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
            public virtual ICollection<Users> Users { get; set; }
        }
   public class UserDBRoles : DropCreateDatabaseAlways<PracticeDB>
    {
        protected override void Seed(PracticeDB db)
        {
            db.Roles.Add(new Roles { RoleID = 1, RoleName = "Admin" });
            db.Roles.Add(new Roles { RoleID = 2, RoleName = "User" });
            db.Users.Add(new Users
            {
                UserID= 3,
                Name = "Admin",
                Password = "123456",
                FirstName = "Admin",
                LastName = "Adminov",
                MiddleName = "Adminovich",
                Email = "administratorya@gamil.com",
                RoleID = 1
            });
            base.Seed(db); 
        }
    } 
}

Global.asax中

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Models
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            Database.SetInitializer(new UserDBRoles());
        }
    } 
}

如果您删除行Database.SetInitializer(new UserDBRoles());然后没有错误,则会对用户进行身份验证。

0 个答案:

没有答案