将登录页和首页html文件拷贝到项目文件夹Views下的Home里
将模板中的css文件和js文件拷贝到项目里
解决方案下新建项目:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
namespace WangQi.EntityFramework.DAL
{
public class DbContextFactory where TS : DbContext, new()
{
public static DbContext GetCarrentDcontext()
{
var dbContext = CallContext.GetData(typeof(TS).Name) as DbContext;
if (dbContext != null)
{
return dbContext;
}
dbContext = new TS();
CallContext.SetData(typeof(TS).Name, dbContext);
return dbContext;
}
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace WangQi.EntityFramework.DAL
{
public class BaseRepository where T : class, new() where TS : DbContext, new()
{
private DbContext dbContext = DbContextFactory.GetCarrentDcontext();
///
/// 通用单表添加方法
///
///
///
public bool Add(T entity)
{
dbContext.Set().Add(entity);
return dbContext.SaveChanges() > 0;
}
///
/// 通用单表删除方法
///
///
///
public bool Delete(T entity)
{
dbContext.Entry(entity).State = EntityState.Deleted;
return dbContext.SaveChanges() > 0;
}
///
/// 通用单表批量删除,通过对象集合
///
///
///
public bool Delete(List entityList)
{
dbContext.Set().RemoveRange(entityList);
return dbContext.SaveChanges() > 0;
}
///
/// 通用单表批量删除,通过id集合
///
///
///
public bool Delete(List ids)
{
foreach (var item in ids)
{
var t = dbContext.Set().Find(item);
dbContext.Set().Remove(t);
}
return dbContext.SaveChanges() > 0;
}
///
/// 通用单表修改方法
///
///
///
public bool Update(T entity)
{
dbContext.Entry(entity).State = EntityState.Modified;
return dbContext.SaveChanges() > 0;
}
///
/// 返回一个数据集合
///
///
public List QueryList(Expression> lamdaExpression)
{
return dbContext.Set().Where(lamdaExpression).ToList();
}
///
/// 返回单个对象
///
///
///
public T Query(Expression> lamdaExpression)
{
return dbContext.Set().Where(lamdaExpression).SingleOrDefault();
}
///
/// 判断对象是否存在
///
///
///
public bool Exist(Expression> lamdaExpression)
{
return dbContext.Set().Where(lamdaExpression).Any();
}
///
/// 分页查询
///
///
///
///
///
///
///
///
public List QueryPageList(int pageIndex, int pageSize, Expression> lamdaExpression, Expression> orderBy, out int count, bool isAcs = true)
{
count = dbContext.Set().Where(lamdaExpression).Count();
if (isAcs)
{
return dbContext.Set().Where(lamdaExpression)
.OrderBy(orderBy)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
}
else
{
return dbContext.Set().Where(lamdaExpression)
.OrderByDescending(orderBy)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize).ToList();
}
}
///
/// 返回总记录数
///
///
///
public int QueryCount(Expression> lamdaExpression)
{
return dbContext.Set().Where(lamdaExpression).Count();
}
}
}
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs"#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\\WangQi.EntityFramework.Model\Model1.edmx"; //右键复制实体层Model1.edmx 文件
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangQi.EntityFramework.DAL;
namespace WangQi.EntityFramework.DAL
{
<#
foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name))
{
#>
public partial class <#=entity.Name#>Repository : BaseRepository<<#=entity.Name#>,WangQiEntities>
{
}
<#}#>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WangQi.EntityFramework.DAL;
using WangQi.EntityFramework.Model;
namespace WangQi.EntityFramework.BLL
{
public class BaseSerivces where T : class, new()
{
public BaseRepository baseRepository = new BaseRepository();
///
/// 通用单表添加方法
///
///
///
public bool Add(T entity)
{
return baseRepository.Add(entity);
}
///
/// 通用单表删除方法
///
///
///
public bool Delete(T entity)
{
return baseRepository.Delete(entity);
}
///
/// 通用单表批量删除,通过对象集合
///
///
///
public bool Delete(List entityList)
{
return baseRepository.Delete(entityList);
}
///
/// 通用单表批量删除,通过id集合
///
///
///
public bool Delete(List ids)
{
return baseRepository.Delete(ids);
}
///
/// 通用单表修改方法
///
///
///
public bool Update(T entity)
{
return baseRepository.Update(entity);
}
///
/// 返回一个数据集合
///
///
public List QueryList(Expression> lamdaExpression)
{
return baseRepository.QueryList(lamdaExpression);
}
///
/// 返回单个对象
///
///
///
public T Query(Expression> lamdaExpression)
{
return baseRepository.Query(lamdaExpression);
}
///
/// 判断对象是否存在
///
///
///
public bool Exist(Expression> lamdaExpression)
{
return baseRepository.Exist(lamdaExpression);
}
///
/// 分页查询
///
///
///
///
///
///
///
///
public List QueryPageList(int pageIndex, int pageSize, Expression> lamdaExpression, Expression> orderBy, out int count, bool isAcs = true)
{
return baseRepository.QueryPageList(pageIndex, pageSize, lamdaExpression, orderBy, out count);
}
///
/// 返回总记录数
///
///
///
public int QueryCount(Expression> lamdaExpression)
{
return baseRepository.QueryCount(lamdaExpression);
}
}
}
///
/// 登陆
///
///
///
public JsonResult Login(AdminInfo adminInfo,bool checkbox)
{
opercte.Success = _adminInfoService.Exist(a => a.AdminName == adminInfo.AdminName && a.AdminPassWord == adminInfo.AdminPassWord);
if (opercte.Success)
{
if (opercte.Success)
{
UserContext.context.user = adminInfo;
}
if (checkbox)
{
//Cookie
HttpCookie cookie = new HttpCookie("key"); //创建Cookie ("随便取名")
cookie["key"] = adminInfo.AdminName;
cookie.Expires = DateTime.Now.AddDays(1); //设置Cookie时间为1天
System.Web.HttpContext.Current.Response.Cookies.Add(cookie); //添加Cookie到浏览器
}
}
return Json(opercte);
}