首次提交:添加src文件夹代码
This commit is contained in:
186
Cowain.Bake.BLL/ServiceBase.cs
Normal file
186
Cowain.Bake.BLL/ServiceBase.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using Cowain.Bake.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.Bake.BLL
|
||||
{
|
||||
public class ServiceBase
|
||||
{
|
||||
public readonly object _lockObj = new object();
|
||||
|
||||
protected IUnityContainer _unityContainer;
|
||||
public ServiceBase(IUnityContainer unityContainer)
|
||||
{
|
||||
//using (var context = new DbContext()) // 创建DbContext
|
||||
//第二层:DbConnection(由ADO.NET连接池管理);password=123456;Pooling=true;Max Pool Size=100;
|
||||
//var data = context.Products.ToList(); // 触发连接获取
|
||||
_unityContainer = unityContainer;
|
||||
}
|
||||
|
||||
//public int Commit()
|
||||
//{
|
||||
// return this.Context.SaveChanges();
|
||||
//}
|
||||
|
||||
public int Delete<T>(int Id) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
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 BakingEntities())
|
||||
{
|
||||
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 BakingEntities())
|
||||
{
|
||||
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 BakingEntities())
|
||||
{
|
||||
return Context.Set<T>().ToList();
|
||||
}
|
||||
}
|
||||
public T Find<T>(int id) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
return Context.Set<T>().Find(id);
|
||||
}
|
||||
}
|
||||
|
||||
public int Insert<T>(T t) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
Context.Set<T>().Add(t);
|
||||
return Context.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public int Insert<T>(IEnumerable<T> tList) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
Context.Set<T>().AddRange(tList);
|
||||
return Context.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
return Context.Set<T>().Where<T>(funcWhere);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int Update<T>(T t) where T : class
|
||||
{
|
||||
if (t == null) throw new Exception("t is null");
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
Context.Set<T>().Attach(t);//将数据附加到上下文,支持实体修改和新实体,重置为UnChanged
|
||||
Context.Entry<T>(t).State = EntityState.Modified;
|
||||
return Context.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Update<T>(IEnumerable<T> tList) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
foreach (var t in tList)
|
||||
{
|
||||
Context.Set<T>().Attach(t);
|
||||
Context.Entry<T>(t).State = EntityState.Modified;
|
||||
}
|
||||
Context.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
public int UpdateListParas<T>(IEnumerable<T> tList) where T : class
|
||||
{
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
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 DataTable GetDataTable(string sql)
|
||||
{
|
||||
lock(_lockObj)
|
||||
{
|
||||
MySqlConnection conn = new MySqlConnection();
|
||||
using (var Context = new BakingEntities())
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user