Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理1

首先给上项目的整体框架图:,这里我没有使用BLL,因为感觉太烦了就没有去使用。

那么接下来我们首先先去Model层中添加Model。

管理员类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 管理员
    /// </summary>
    public class Admin {
        /// <summary>
        /// 管理员ID(主键)
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [StringLength(30)]
        [Column(TypeName="varchar")]
        public string AdminName { get; set; }
        /// <summary>
        /// 手机号码
        /// </summary>
        [StringLength(11)]
        [Column(TypeName = "varchar")]
        public string Mobile { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        [StringLength(100)]
        [Column(TypeName = "varchar")]
        public string Email { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        [StringLength(120)]
        [Column(TypeName = "varchar")]
        public string Password { get; set; }
        /// <summary>
        /// 密码盐值
        /// </summary>
        [StringLength(10)]
        [Column(TypeName = "varchar")]
        public string Salt { get; set; }
        /// <summary>
        /// 对应角色Id
        /// </summary>
        public int AuthoryId { get; set; }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 是否超级管理员
        /// </summary>
        public Byte IsSuperAdmin { get; set; }
        /// <summary>
        /// 上次登录时间
        /// </summary>
        public DateTime LastLoginTime { get; set; }
        /// <summary>
        /// 上次登录地址
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "nvarchar")]
        public string LastLoginAddress { get; set; }
        /// <summary>
        /// 上次登录端口基本信息
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string LastLoginInfo { get; set; }
        /// <summary>
        /// 上次登录Ip
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string LastLoginIp { get; set; }
        public Admin(){
            CreateTime = DateTime.Now;
            LastLoginTime = DateTime.Now;
        }
    }
}
View Code

[Key]这个事用来设置主键的,[StringLength(30)]是用来控制在创建数据库的时候控制长度,[Column(TypeName="varchar")]是用来创建该字段在数据库中的类型。我在创建的时候没有使用外键,因本人对外键不熟所以没使用。Byte类型就相当于数据库的tinyint类型。

页面与页面动作联系表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 页面与页面动作联系表
    /// </summary>
    public class ActionToPage {
        /// <summary>
        /// 自增Id
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 页面Id
        /// </summary>
        public int PageId { get; set; }
        /// <summary>
        /// 动作集合(用json存储)
        /// </summary>
        [StringLength(300)]
        [Column(TypeName = "varchar")]
        public string ActionList { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        public Byte IsDelete { get; set; }
    }
}
View Code

管理员登录日志:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 管理员登录日志
    /// </summary>
    public class AdminLoginLog {
        /// <summary>
        /// 管理员登录记录Id
        /// </summary>
        [Key]
        public int AdminLoginLogId { get; set; }
        /// <summary>
        /// 管理员Id
        /// </summary>
        public int AdminId { get; set; }
        /// <summary>
        /// 管理员登录地址
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "nvarchar")]
        public string AdminLoginAddress { get; set; }
        /// <summary>
        /// 管理员登录IP
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string AdminLoginIP { get; set; }
        /// <summary>
        /// 管理员登录时间
        /// </summary>
        public DateTime AdminLoginTime { get; set; }
        /// <summary>
        /// 浏览器信息或者APP信息
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string AdminLoginInfo { get; set; }
        public AdminLoginLog() {
            AdminLoginTime = DateTime.Now;
        }
    }
}
View Code

管理员操作类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 管理员操作类
    /// </summary>
    public class AdminOperation {
        [Key]
        public int AdminOperationId { get; set; }
        /// <summary>
        /// 管理员Id
        /// </summary>
        public int AdminId { get; set; }
        /// <summary>
        /// 权限Id
        /// </summary>
        public int AuthoryId { get; set; }
        /// <summary>
        /// 动作Id(1:增,2:修改,3:查看,4:删除)
        /// </summary>
        public int Action { get; set; }
        /// <summary>
        /// 操作标题
        /// </summary>
        [StringLength(15)]
        [Column(TypeName = "nvarchar")]
        public string Title { get; set; }
        /// <summary>
        /// 操作详情
        /// </summary>
        [StringLength(200)]
        [Column(TypeName = "nvarchar")]
        public string Content { get; set; }
        /// <summary>
        /// 操作IP
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string OperateIP { get; set; }
        /// <summary>
        /// 操作地址
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "nvarchar")]
        public string OperateAddress { get; set; }
        /// <summary>
        /// 浏览器信息或者APP信息
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string OperateInfo { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        public AdminOperation() {
            CreateTime = DateTime.Now;
        }
    }
}
View Code

用户与页面和页面动作联系表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 用户与页面和页面动作联系表
    /// </summary>
    public class AdminToPage {
        /// <summary>
        /// 
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 管理员Id
        /// </summary>
        public int AdminId { get; set; }
        /// <summary>
        /// 页面Id
        /// </summary>
        public int PageId { get; set; }
        /// <summary>
        /// 动作集合(用json存储)
        /// </summary>
        [StringLength(300)]
        [Column(TypeName = "varchar")]
        public string ActionList { get; set; }
        /// <summary>
        /// 是否显示
        /// </summary>
        public Byte IsShow { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        public Byte IsDelete { get; set; }
    }
}
View Code

角色:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 角色
    /// </summary>
    public class Authory {
        /// <summary>
        /// 角色Id
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 角色名称
        /// </summary>
        [StringLength(15)]
        [Column(TypeName = "varchar")]
        public string Name { get; set; }
        /// <summary>
        /// 角色简介
        /// </summary>
        [StringLength(30)]
        [Column(TypeName = "varchar")]
        public string Intro { get; set; }
        /// <summary>
        /// 角色状态(0:正常;1:已删除)
        /// </summary>
        public Byte State { get; set; }
    }
}
View Code

角色与页面和页面动作之间联系:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 角色与页面和页面动作之间联系
    /// </summary>
    public class AuthoryToPage {
        /// <summary>
        /// 自增Id
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 权限Id
        /// </summary>
        public int AuthoryId { get; set; }
        /// <summary>
        /// 页面Id
        /// </summary>
        public int PageId { get; set; }
        /// <summary>
        /// 动作集合(用json存储)
        /// </summary>
        [StringLength(300)]
        [Column(TypeName = "varchar")]
        public string ActionList { get; set; }
        /// <summary>
        /// 是否显示
        /// </summary>
        public Byte IsShow { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        public Byte IsDelete { get; set; }
    }
}
View Code

页面动作:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 页面动作
    /// </summary>
    public class PageAction {
        /// <summary>
        /// 动作Id
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 动作名称
        /// </summary>
        [StringLength(15)]
        [Column(TypeName = "varchar")]
        public string Name { get; set; }
        /// <summary>
        /// 动作代码(页面代码)
        /// </summary>
        [StringLength(35)]
        [Column(TypeName = "varchar")]
        public string ActionCode { get; set; }
        /// <summary>
        /// 是否显示
        /// </summary>
        public Byte IsShow { get; set; }
        /// <summary>
        /// 动作等级
        /// </summary>
        public Byte ActionLevel { get; set; }
    }
}
View Code

页面:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.Model {
    /// <summary>
    /// 页面
    /// </summary>
    public class PageMenu {
        /// <summary>
        /// 
        /// </summary>
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 页面Id
        /// </summary>
        public int PId { get; set; }
        /// <summary>
        /// 页面名称
        /// </summary>
        [StringLength(15)]
        [Column(TypeName = "varchar")]
        public string Name { get; set; }
        /// <summary>
        /// 页面路径
        /// </summary>
        [StringLength(50)]
        [Column(TypeName = "varchar")]
        public string PageUrl { get; set; }
        /// <summary>
        /// 是否显示
        /// </summary>
        public byte IsShow { get; set; }
        /// <summary>
        /// 排序
        /// </summary>
        public int OrderNum { get; set; }
        /// <summary>
        /// 页面图标
        /// </summary>
        [StringLength(30)]
        [Column(TypeName = "varchar")]
        public string Ico { get; set; }
    }
}
View Code

以上就是Model了。

下面的是对基础仓储的编辑(也就是DAL):

首先安装下EF,选择6.0安装即可

首先是DbContext:这是EF与数据库连接的上下文。

using AuthorDesign.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthorDesign.DAL {
    public class AuthorDesignContext : DbContext {
        public AuthorDesignContext()
            : base("AuthorDesignContext") { 
        }

        public DbSet<ActionToPage> ActionToPages { get; set; }
        public DbSet<Admin> Admins { get; set; }
        public DbSet<AdminLoginLog> AdminLoginLogs { get; set; }
        public DbSet<AdminOperationRepository> AdminOperations { get; set; }
        public DbSet<AdminToPage> AdminToPages { get; set; }
        public DbSet<Authory> Authories { get; set; }
        public DbSet<AuthoryToPage> AuthoryToPages { get; set; }
        public DbSet<PageAction> PageActions { get; set; }
        public DbSet<PageMenu> PageMenus { get; set; }
    }
}
View Code

感觉类有点多,下篇继续写关于DAL的,这里一些都是简单的代码。可能本人写的不好,各位看官要是那里可以改进的或者那里有疑问的可以问我。

百度源码下载地址

posted @ 2015-11-22 22:08  yjq_net  阅读(3831)  评论(8编辑  收藏  举报