日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册

發布時間:2025/3/8 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面

上篇文章簡單介紹了項目的結構,這篇文章將實現用戶的注冊。當然關于漂亮的ui,這在追后再去添加了,先將功能實現。也許代碼中有不合適的地方,也只有在之后慢慢去優化了。

系列文章

[EF]vs15+ef6+mysql code first方式

[實戰]MVC5+EF6+MySql企業網盤實戰(1)

[實戰]MVC5+EF6+MySql企業網盤實戰(2)——用戶注冊

實現

Model層

UserInfo實體模型

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations; using Wolfy.NetDisk.Utilities;namespace Wolfy.NetDisk.Model {/// <summary>/// 用戶信息類/// </summary>public class UserInfo{/// <summary>/// 編號/// </summary> [Key]public int Id { set; get; }/// <summary>/// 用戶頭像地址/// </summary>[StringLength(512)][Display(Name = "頭像")]public string Header { set; get; }/// <summary>/// 姓名/// </summary>[MaxLength(64, ErrorMessage = "網名長度不得超過32個字符")][Required(ErrorMessage = "請填寫您的名稱")][Display(Name = "姓名")]public string Name { set; get; }/// <summary>/// 網名/// </summary>[MaxLength(64, ErrorMessage = "網名長度不得超過32個字符")][Required(ErrorMessage = "請填寫您的網名")][Display(Name = "網名")]public string DisplayName { set; get; }/// <summary>/// 郵箱/// </summary>[RegularExpression(@"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$", ErrorMessage = "請輸入正確的郵箱地址")][MaxLength(2048, ErrorMessage = "網名長度不得超過2048個字符")][Required][Display(Name = "郵箱")]public string Email { set; get; }/// <summary>/// 婚姻情況/// </summary>[Display(Name = "婚姻狀況")]public MarriageType Marriage { set; get; }/// <summary>/// 政治面貌/// </summary>[Display(Name = "政治面貌")]public PoliticalStatus PoliticalStatus { set; get; }/// <summary>/// 學歷/// </summary>[Display(Name = "學歷")]public XueliType Xueli { set; get; }/// <summary>/// 職位/// </summary>[MaxLength(128)][Display(Name = "職位")]public string Position { set; get; }/// <summary>/// 電話/// </summary>[MaxLength(32)][Display(Name = "電話")]public string Tel { set; get; }/// <summary>/// 密碼/// </summary>[StringLength(32, ErrorMessage = "密碼長度不得超多32位")][Required][Display(Name = "密碼")]public string Pwd { set; get; }/// <summary>/// 生日/// </summary>[Display(Name = "生日")]public DateTime Birthday { set; get; } = DateTime.Now;/// <summary>/// 性別/// </summary>[Display(Name = "性別")]public GenderType Gender { set; get; }/// <summary>/// 住址/// </summary>[MaxLength(32)][Display(Name = "地址")]public string Address { set; get; }/// <summary>/// 籍貫/// </summary>[MaxLength(32)][Display(Name = "籍貫")]public string Hometown { set; get; }/// <summary>/// 公司/// </summary>[StringLength(512, ErrorMessage = "公司名稱超過了512字符")][Display(Name = "公司名稱")]public string Company { set; get; }/// <summary>/// 所屬部門id/// </summary>[Display(Name = "部門")]public int DepartmentId { set; get; }/// <summary>/// 添加時間/// </summary>[Display(Name = "日期")]public DateTime Dt { set; get; } = DateTime.Now;} } UserInfo

IDAL層

添加泛型接口IBaseRepository<TEntity>,存放一些常用的操作,增刪改查等。

/// <summary>/// 倉儲基類泛型接口/// </summary>/// <typeparam name="TEntity"></typeparam>public interface IBaseRepository<TEntity> : IDisposable{/// <summary>/// 添加實體/// </summary>/// <param name="entity"></param>/// <returns></returns> TEntity Add(TEntity entity);/// <summary>/// 計數/// </summary>/// <param name="where"></param>/// <returns></returns>int Count(Expression<Func<TEntity, bool>> where);/// <summary>/// 更新/// </summary>/// <param name="entity"></param>/// <returns></returns> TEntity Update(TEntity entity);bool Delete(TEntity entity);/// <summary>/// 是否存在/// </summary>/// <param name="where"></param>/// <returns></returns>bool Exist(Expression<Func<TEntity, bool>> where);/// <summary>/// 條件查詢/// </summary>/// <param name="where"></param>/// <returns></returns>TEntity Find(Expression<Func<TEntity, bool>> where);/// <summary>/// 查詢集合/// </summary>/// <param name="where"></param>/// <returns></returns>IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where);/// <summary>/// 條件查詢/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="where"></param>/// <param name="isAsc">是否升序</param>/// <param name="orderlanbda">排序表達式</param>/// <returns></returns>IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda);/// <summary>/// 分頁查詢/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="totalRecord"></param>/// <param name="where"></param>/// <param name="isAsc"></param>/// <param name="orderLambda"></param>/// <returns></returns>IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda);/// <summary>/// 保存/// </summary>/// <returns></returns>int SaveChanges();} IBaseRepository

IUserInfoRepository:UserInfo操作接口。

/// <summary>/// 用戶信息倉儲接口/// </summary>public interface IUserInfoRepository : IBaseRepository<UserInfo>{} IUserInfoRepository

DAL層

添加數據庫上下文NetDiskContext類。關于ef6+mysql code first的具體使用可以參考前面的文章。

/// <summary>/// 數據庫上下文/// </summary>public class NetDiskContext : DbContext{/// <summary>/// name:數據庫連接字符串/// </summary>public NetDiskContext(): base("name=NetDiskContext"){}public DbSet<UserInfo> UserInfos { set; get; }//public DbSet<Department> Deparments { set; get; }//public DbSet<Model.NetDisk> NetDisks { set; get; }} NetDiskContext

?ContextFactory:用來獲取數據上下文的工廠,代碼如下,第一次使用ef,也不知道有沒有更好的實現方式,先這樣實現吧,以后發現更好的方式,再進行優化。

/// <summary>/// 數據上下文工廠類/// </summary>public static class ContextFactory{/// <summary>/// 獲取數據庫上下文/// </summary>/// <returns></returns>public static DbContext GetDbContext(){NetDiskContext _netDiskContext = CallContext.GetData("NetDiskContext") as NetDiskContext;if (_netDiskContext == null){_netDiskContext = new NetDiskContext();IDatabaseInitializer<NetDiskContext> dbInitializer = null;if (_netDiskContext.Database.Exists()){//如果數據庫已經存在dbInitializer = new DropCreateDatabaseIfModelChanges<NetDiskContext>();}else{//總是先刪除然后再創建dbInitializer = new DropCreateDatabaseAlways<NetDiskContext>();}dbInitializer.InitializeDatabase(_netDiskContext);CallContext.SetData("NetDiskContext", _netDiskContext);return _netDiskContext;}return _netDiskContext;}} ContextFactory

BaseRepository:泛型基類,實現接口IBaseRepository

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.DAL {/// <summary>/// 倉儲基類/// </summary>public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class{protected NetDiskContext netDiskContext = ContextFactory.GetDbContext() as NetDiskContext;public TEntity Add(TEntity entity){netDiskContext.Entry<TEntity>(entity).State = System.Data.Entity.EntityState.Added;return entity;}public int Count(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Count(where);}public bool Delete(TEntity entity){netDiskContext.Entry<TEntity>(entity).State = System.Data.Entity.EntityState.Deleted;return this.SaveChanges() > 0;}public void Dispose(){if (netDiskContext != null){netDiskContext.Dispose();GC.SuppressFinalize(netDiskContext);}}public bool Exist(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Any(where);}public TEntity Find(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().FirstOrDefault(where);}public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where){return netDiskContext.Set<TEntity>().Where(where);}public IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda){var lst = netDiskContext.Set<TEntity>().Where<TEntity>(where);if (!isAsc){lst = lst.OrderByDescending<TEntity, SEntity>(orderlanbda);}return lst;}public IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda){var lst = netDiskContext.Set<TEntity>().Where<TEntity>(where);totalRecord = lst.Count();if (!isAsc){lst = lst.OrderByDescending<TEntity, SEntity>(orderLambda);}return lst.Skip<TEntity>((pageIndex - 1) * pageIndex).Take(pageSize);}public int SaveChanges(){return netDiskContext.SaveChanges();}public TEntity Update(TEntity entity){TEntity tentity = netDiskContext.Set<TEntity>().Attach(entity);netDiskContext.Entry<TEntity>(entity).State = System.Data.Entity.EntityState.Modified;return tentity;}} } BaseRepository /// <summary>/// 用戶數據操作dal層 /// </summary>public class UserInfoRepository:BaseRepository<UserInfo>, IUserInfoRepository{} UserInfoRepository

倉儲工廠,用來獲取具體的倉儲接口。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Wolfy.NetDisk.IDAL; using Wolfy.NetDisk.Model;namespace Wolfy.NetDisk.DAL {/// <summary>/// 倉儲工廠/// </summary>public static class RepositoryFactory{public static IUserInfoRepository UserInfoRepository { get { return new UserInfoRepository(); } }} } RepositoryFactory

IBLL層

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks;namespace Wolfy.NetDisk.IBLL {public interface IBaseServiceRepository<TEntity>{/// <summary>/// 添加實體/// </summary>/// <param name="entity"></param>/// <returns></returns> TEntity Add(TEntity entity);/// <summary>/// 計數/// </summary>/// <param name="where"></param>/// <returns></returns>int Count(Expression<Func<TEntity, bool>> where);/// <summary>/// 更新/// </summary>/// <param name="entity"></param>/// <returns></returns> TEntity Update(TEntity entity);bool Delete(TEntity entity);/// <summary>/// 是否存在/// </summary>/// <param name="where"></param>/// <returns></returns>bool Exist(Expression<Func<TEntity, bool>> where);/// <summary>/// 條件查詢/// </summary>/// <param name="where"></param>/// <returns></returns>TEntity Find(Expression<Func<TEntity, bool>> where);/// <summary>/// 查詢集合/// </summary>/// <param name="where"></param>/// <returns></returns>IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where);/// <summary>/// 條件查詢/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="where"></param>/// <param name="isAsc">是否升序</param>/// <param name="orderlanbda">排序表達式</param>/// <returns></returns>IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda);/// <summary>/// 分頁查詢/// </summary>/// <typeparam name="SEntity"></typeparam>/// <param name="pageIndex"></param>/// <param name="pageSize"></param>/// <param name="totalRecord"></param>/// <param name="where"></param>/// <param name="isAsc"></param>/// <param name="orderLambda"></param>/// <returns></returns>IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda);int SaveChanges();} } IBaseServiceRepository using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Wolfy.NetDisk.Model;namespace Wolfy.NetDisk.IBLL {public interface IUserInfoServiceRepository:IBaseServiceRepository<UserInfo>{} } IUserInfoServiceRepository

BLL層

using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Wolfy.NetDisk.IBLL; using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.BLL {public class BaseServiceRepository<TEntity> : IBaseServiceRepository<TEntity> where TEntity : class,new(){protected IBaseRepository<TEntity> currentRepository { set; get; }public BaseServiceRepository(IBaseRepository<TEntity> currentRepository){this.currentRepository = currentRepository;}public TEntity Add(TEntity entity){return currentRepository.Add(entity);}public int Count(Expression<Func<TEntity, bool>> where){return currentRepository.Count(where);}public bool Delete(TEntity entity){return currentRepository.Delete(entity);}public bool Exist(Expression<Func<TEntity, bool>> where){return currentRepository.Exist(where);}public TEntity Find(Expression<Func<TEntity, bool>> where){return currentRepository.Find(where);}public IQueryable<TEntity> FindAll(Expression<Func<TEntity, bool>> where){return currentRepository.FindAll(where);}public IQueryable<TEntity> FindAll<SEntity>(Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderlanbda){return currentRepository.FindAll<SEntity>(where, isAsc, orderlanbda);}public IQueryable<TEntity> FindPaged<SEntity>(int pageIndex, int pageSize, out int totalRecord, Expression<Func<TEntity, bool>> where, bool isAsc, Expression<Func<TEntity, SEntity>> orderLambda){return currentRepository.FindPaged<SEntity>(pageIndex, pageSize, out totalRecord, where, isAsc, orderLambda);}public int SaveChanges(){return currentRepository.SaveChanges();}public TEntity Update(TEntity entity){return currentRepository.Update(entity);}} } BaseServiceRepository using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Wolfy.NetDisk.Model; using Wolfy.NetDisk.DAL; using Wolfy.NetDisk.IBLL; using Wolfy.NetDisk.IDAL;namespace Wolfy.NetDisk.BLL {public class UserInfoServiceRepository : BaseServiceRepository<UserInfo>, IUserInfoServiceRepository{/// <summary>/// 構造函數,通過倉儲工廠調用dal中的具體的倉儲/// </summary>/// <param name="currentRepository"></param>public UserInfoServiceRepository(): base(RepositoryFactory.UserInfoRepository){}} } UserInfoServiceRepository

UI層

添加UserInfo控制器

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; using Wolfy.NetDisk.Model; using Wolfy.NetDisk.IBLL; using Wolfy.NetDisk.BLL; namespace Wolfy.NetDisk.Site.Controllers {public class UserInfoController : AsyncController{private IUserInfoServiceRepository _userInfoServiceRepository = new UserInfoServiceRepository();/// <summary>/// 用戶信息列表/// </summary>/// <returns></returns>public ActionResult Users(){var users = _userInfoServiceRepository.FindAll(x => x.DisplayName != "");return View(users);}[HttpGet]public ActionResult Register(){return View();}[HttpPost]public ActionResult Register(UserInfo userInfo){if (ModelState.IsValid){_userInfoServiceRepository.Add(userInfo);_userInfoServiceRepository.SaveChanges();}return RedirectToAction("Users");}} } UserInfoController

?添加視圖

先不管界面的美與丑,先試下能否正確的添加數據。如果成功下一步,再進行美化。

總結

?下面將完善注冊的過程,用戶名是否存在驗證,密碼加密,驗證碼等操作。

?

轉載于:https://www.cnblogs.com/wolf-sun/p/4823280.html

總結

以上是生活随笔為你收集整理的[实战]MVC5+EF6+MySql企业网盘实战(2)——用户注册的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。