185 lines
5.7 KiB
C#
185 lines
5.7 KiB
C#
using Cowain.Preheat.Model;
|
|
using MySql.Data.MySqlClient;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using Unity;
|
|
|
|
namespace Cowain.Preheat.BLL
|
|
{
|
|
public class ServiceBase
|
|
{
|
|
public readonly object _lockObj = new object();
|
|
//protected DbContext Context { get; private set; }
|
|
protected IUnityContainer _unityContainer;
|
|
public ServiceBase(IUnityContainer unityContainer)
|
|
{
|
|
//Context = new PreheatEntities();
|
|
_unityContainer = unityContainer;
|
|
}
|
|
//public int Commit()
|
|
//{
|
|
// return this.Context.SaveChanges();
|
|
//}
|
|
|
|
public int Delete<T>(int Id) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
T t = Context.Set<T>().Find(Id);//也可以附加
|
|
if (t == null) throw new Exception("t is null");
|
|
Context.Set<T>().Remove(t);
|
|
return Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public int Delete<T>(T t) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
if (t == null) throw new Exception("t is null");
|
|
Context.Set<T>().Attach(t);
|
|
Context.Set<T>().Remove(t);
|
|
return Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public void Delete<T>(IEnumerable<T> tList) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
foreach (var t in tList)
|
|
{
|
|
Context.Set<T>().Attach(t);
|
|
}
|
|
Context.Set<T>().RemoveRange(tList);
|
|
Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public List<T> Find<T>() where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<T>().ToList();
|
|
}
|
|
}
|
|
public T Find<T>(int id) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<T>().Find(id);
|
|
}
|
|
}
|
|
|
|
public int Insert<T>(T t) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
Context.Set<T>().Add(t);
|
|
return Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public int Insert<T>(IEnumerable<T> tList) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
Context.Set<T>().AddRange(tList);
|
|
return Context.SaveChanges();//写在这里 就不需要单独commit 不写就需要
|
|
}
|
|
}
|
|
|
|
public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Set<T>().Where<T>(funcWhere);
|
|
}
|
|
}
|
|
|
|
public int Update<T>(T t) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
if (t == null) throw new Exception("t is null");
|
|
Context.Set<T>().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged
|
|
Context.Entry<T>(t).State = EntityState.Modified;
|
|
return Context.SaveChanges();
|
|
}
|
|
|
|
}
|
|
|
|
public bool Update<T>(IEnumerable<T> tList) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
foreach (var t in tList)
|
|
{
|
|
Context.Set<T>().Attach(t);
|
|
Context.Entry<T>(t).State = EntityState.Modified;
|
|
}
|
|
return Context.SaveChanges()> 0 ? true : false;
|
|
}
|
|
|
|
}
|
|
public int UpdateListParas<T>(IEnumerable<T> tList) where T : class
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
foreach (var t in tList)
|
|
{
|
|
Context.Set<T>().Attach(t);
|
|
Context.Entry<T>(t).State = EntityState.Modified;
|
|
}
|
|
return Context.SaveChanges();
|
|
}
|
|
|
|
}
|
|
public virtual void Dispose()
|
|
{
|
|
//if (this.Context != null)
|
|
//{
|
|
// this.Context.Dispose();
|
|
//}
|
|
}
|
|
public int ExecuteNonQuery(string sql)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
return Context.Database.ExecuteSqlCommand(sql);
|
|
}
|
|
}
|
|
public DataTable GetDataTable(string sql)
|
|
{
|
|
lock(_lockObj)
|
|
{
|
|
using (var Context = new PreheatEntities())
|
|
{
|
|
MySqlConnection conn = new MySqlConnection();
|
|
conn.ConnectionString = Context.Database.Connection.ConnectionString;
|
|
if (conn.State != ConnectionState.Open)
|
|
{
|
|
conn.Open();
|
|
}
|
|
MySqlCommand cmd = new MySqlCommand();
|
|
cmd.Connection = conn;
|
|
cmd.CommandText = sql;
|
|
|
|
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
|
|
DataTable table = new DataTable();
|
|
adapter.Fill(table);
|
|
|
|
conn.Close();//连接需要关闭
|
|
conn.Dispose();
|
|
return table;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|