101 lines
2.8 KiB
C#
101 lines
2.8 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Linq.Expressions;
|
|||
|
|
using RIZO.Server.IService;
|
|||
|
|
using RIZO.Server.IEFContext;
|
|||
|
|
|
|||
|
|
namespace RIZO.Server.Service
|
|||
|
|
{
|
|||
|
|
public class ServiceBase : IServiceBase
|
|||
|
|
{
|
|||
|
|
protected DbContext Context { get; private set; }
|
|||
|
|
|
|||
|
|
public ServiceBase(RIZO.Server.IEFContext.IEFContext eFContext)
|
|||
|
|
{
|
|||
|
|
Context = eFContext.CreateDBContext();
|
|||
|
|
}
|
|||
|
|
public void Commit()
|
|||
|
|
{
|
|||
|
|
this.Context.SaveChanges();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Delete<T>(int Id) where T : class
|
|||
|
|
{
|
|||
|
|
T t = this.Find<T>(Id);//也可以附加
|
|||
|
|
if (t == null) throw new Exception("t is null");
|
|||
|
|
this.Context.Set<T>().Remove(t);
|
|||
|
|
this.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Delete<T>(T t) where T : class
|
|||
|
|
{
|
|||
|
|
if (t == null) throw new Exception("t is null");
|
|||
|
|
this.Context.Set<T>().Attach(t);
|
|||
|
|
this.Context.Set<T>().Remove(t);
|
|||
|
|
this.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Delete<T>(IEnumerable<T> tList) where T : class
|
|||
|
|
{
|
|||
|
|
foreach (var t in tList)
|
|||
|
|
{
|
|||
|
|
this.Context.Set<T>().Attach(t);
|
|||
|
|
}
|
|||
|
|
this.Context.Set<T>().RemoveRange(tList);
|
|||
|
|
this.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public T Find<T>(int id) where T : class
|
|||
|
|
{
|
|||
|
|
return this.Context.Set<T>().Find(id);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public T Insert<T>(T t) where T : class
|
|||
|
|
{
|
|||
|
|
this.Context.Set<T>().Add(t);
|
|||
|
|
this.Commit();
|
|||
|
|
return t;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class
|
|||
|
|
{
|
|||
|
|
this.Context.Set<T>().AddRange(tList);
|
|||
|
|
this.Commit();//写在这里 就不需要单独commit 不写就需要
|
|||
|
|
return tList;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
|
|||
|
|
{
|
|||
|
|
return this.Context.Set<T>().Where<T>(funcWhere);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Update<T>(T t) where T : class
|
|||
|
|
{
|
|||
|
|
if (t == null) throw new Exception("t is null");
|
|||
|
|
|
|||
|
|
this.Context.Set<T>().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged
|
|||
|
|
this.Context.Entry<T>(t).State = EntityState.Modified;
|
|||
|
|
this.Commit();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Update<T>(IEnumerable<T> tList) where T : class
|
|||
|
|
{
|
|||
|
|
foreach (var t in tList)
|
|||
|
|
{
|
|||
|
|
this.Context.Set<T>().Attach(t);
|
|||
|
|
this.Context.Entry<T>(t).State = EntityState.Modified;
|
|||
|
|
}
|
|||
|
|
this.Commit();
|
|||
|
|
}
|
|||
|
|
public virtual void Dispose()
|
|||
|
|
{
|
|||
|
|
if (this.Context != null)
|
|||
|
|
{
|
|||
|
|
this.Context.Dispose();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|