用EF 搭建底層
接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
namespace Project.DAL
{
interface IBaseService<T> where T:class,new()
{
T Find(params object[] keys);
IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where);
IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where);
IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where);
void Insert(T t);
void Insert(IEnumerable<T> t);
void Update(T t);
void Update(IEnumerable<T> t);
void Delete(T t);
void Delete(IEnumerable<T> t);
bool SaveChange();
}
}
底層
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
namespace Project.DAL
{
public class BaseService<T> : IBaseService<T> where T : class, new()
{
Models.MyAll1817Entities dbContext = DbContextFactory.Create() as Models.MyAll1817Entities;
public T Find(params object[] keys)
{
return dbContext.Set<T>().Find(keys);
}
public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where)
{
IQueryable<T> iq = dbContext.Set<T>();
if (where != null || where.Length < 1)
{
foreach (var item in where)
{
iq = iq.Where(item);
}
}
return iq;
}
public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where)
{
var iq = QueryAll(where);
if (isAsc)
{
iq = iq.OrderBy(order);
}
else
{
iq = iq.OrderByDescending(order);
}
return iq;
}
public IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where)
{
var iq = QueryAll<type>(order, isAsc, where);
total = iq.Count();
return iq.Skip(skip).Take(take);
}
public void Insert(T t)
{
dbContext.Set<T>().Add(t);
}
public void Insert(IEnumerable<T> t)
{
dbContext.Set<T>().AddRange(t);
}
public void Update(T t)
{
dbContext.Entry<T>(t).State = EntityState.Modified;
}
public void Update(IEnumerable<T> t)
{
foreach (var item in t)
{
dbContext.Entry<T>(item).State = EntityState.Modified;
}
}
public void Delete(T t)
{
dbContext.Set<T>().Remove(t);
}
public void Delete(IEnumerable<T> t)
{
dbContext.Set<T>().RemoveRange(t);
}
/// <summary>
/// 保存修改
/// </summary>
/// <returns></returns>
public bool SaveChange()
{
//如下try註釋刪掉的話Debug時不會報錯
//請正式上線時刪掉註釋
//try
//{
dbContext.SaveChanges();
return true;
//}
//catch (Exception e)
//{
// return false;
//}
}
}
}
保證EF上下文對象在線程內是惟一的
這裏比linq要多一個類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Project.Models;
namespace Project.DAL
{
public class DbContextFactory
{
/// <summary>
/// 建立EF上下文對象,已存在就直接取,不存在就建立,保證線程內是惟一。
/// </summary>
public static DbContext Create()
{
DbContext dbContext = CallContext.GetData("DbContext") as DbContext;
if (dbContext == null)
{
dbContext = new MyAll1817Entities ();
CallContext.SetData("DbContext", dbContext);
}
return dbContext;
}
}
}