EntityFramework DB操作帮助类
来源:互联网 发布:mac ruby安装 编辑:程序博客网 时间:2024/06/10 13:32
using LSALogger;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace LSADataAccess
{
public class DataAccessorHelper
{
private readonly static string formatString = typeof(DataAccessorHelper).FullName;
#region Select Method /// <summary> /// Select Single /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <returns></returns> public T SelectSingle<T>(Func<T, bool> selector = null) where T : class { T result = null; try { using (DataAccessorEntities context = new DataAccessorEntities()) { if (selector == null) { result = context.Set<T>().FirstOrDefault(); } else { result = context.Set<T>().FirstOrDefault(selector); } } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } /// <summary> /// Select Single Join Table /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <param name="includePath"></param> /// <returns></returns> public T SelectSingle<T>(Func<T, bool> selector, string includePath) where T : class { T result = null; try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { result = context.CreateObjectSet<T>() .Include(includePath) .Where(selector) .FirstOrDefault(); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } /// <summary> /// Select Single Join Tables /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <param name="includePaths"></param> /// <returns></returns> public T SelectSingle<T>(Func<T, bool> selector, string[] includePaths) where T : class { T result = null; try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1"); if (includePaths != null && includePaths.Count() > 0) { foreach (string path in includePaths) { query = query.Include(path); } } result = query.FirstOrDefault(selector); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } /// <summary> /// Select All /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public IEnumerable<T> Select<T>() where T : class { try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { return context.CreateObjectSet<T>().ToList(); } } catch (Exception ex) { PrintExceptionLog(ex); return null; } } /// <summary> /// Select Collection /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <returns></returns> public IEnumerable<T> Select<T>(Func<T, bool> selector) where T : class { IEnumerable<T> result = null; try { using (DataAccessorEntities context = new DataAccessorEntities()) { result = context.Set<T>().Where(selector).ToList(); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } /// <summary> /// Select Collection Join Table /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <param name="include"></param> /// <returns></returns> public IEnumerable<T> Select<T>(Func<T, bool> selector, string include) where T : class { IEnumerable<T> result = null; try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1"); var data = context.CreateObjectSet<T>() .Include(include) .Where(selector); result = data.ToList(); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } /// <summary> /// Select Collection Join Tables /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <param name="includes"></param> /// <returns></returns> public IEnumerable<T> Select<T>(Func<T, bool> selector, string[] includes) where T : class { IEnumerable<T> result = null; try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { ObjectQuery<T> query = context.CreateObjectSet<T>().Where("1==1"); if (includes != null && includes.Count() > 0) { foreach (string path in includes) { query = query.Include(path); } } result = query.Where(selector).ToList(); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } #endregion #region Insert Method /// <summary> /// Insert Single /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public int Insert<T>(T entity) where T : class { try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { context.CreateObjectSet<T>().AddObject(entity); return context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); } #region other way //using (DataAccessorEntities context = new DataAccessorEntities()) //{ // context.Set<T>().Add(entity); // return context.SaveChanges(); //} #endregion } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } /// <summary> /// Insert Collection /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entities"></param> /// <returns></returns> public int Insert<T>(IEnumerable<T> entities) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { context.Set<T>().AddRange(entities); return context.SaveChanges(); } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } #endregion #region Update Method /// <summary> /// Update Single /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public int Update<T>(T entity) where T : class { try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { ObjectSet<T> objectSet = context.CreateObjectSet<T>(); objectSet.Attach(entity); context.ObjectStateManager.ChangeObjectState(entity, System.Data.Entity.EntityState.Modified); return context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); } #region other way //using (DataAccessorEntities context = new DataAccessorEntities()) //{ // context.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; // return context.SaveChanges(); //} #endregion } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } /// <summary> /// Update Collection /// See better way: https://github.com/loresoft/EntityFramework.Extended /// NuGet: https://www.nuget.org/packages/EntityFramework.Extended /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entities"></param> /// <returns></returns> public int Update<T>(IEnumerable<T> entities) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { foreach (var item in entities) { context.Entry<T>(item).State = EntityState.Modified; } return context.SaveChanges(); } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } #endregion #region Delete Method public int Delete<T>(T entity) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { context.Entry<T>(entity).State = EntityState.Deleted; return context.SaveChanges(); } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } /// <summary> /// Delete Collection /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entities"></param> /// <returns></returns> public int Delete<T>(IEnumerable<T> entities) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { DbSet<T> dbSet = context.Set<T>(); foreach (var item in entities) { dbSet.Attach(item); } dbSet.RemoveRange(entities); return context.SaveChanges(); } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } public int Delete<T>(Func<T, bool> selector) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { IEnumerable<T> deleteData = context.Set<T>().Where(selector); foreach (var item in deleteData) { context.Set<T>().Attach(item); } context.Set<T>().RemoveRange(deleteData); return context.SaveChanges(); } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } #endregion #region Other Method /// <summary> /// Whether the specified object exists /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <returns>-1:Exception, 1: Existed, 0: Not Exist</returns> public int IsExists<T>(Func<T, bool> selector) where T : class { try { using (DataAccessorEntities context = new DataAccessorEntities()) { return context.Set<T>().Where(selector).Count() > 0 ? 1 : 0; } } catch (Exception ex) { PrintExceptionLog(ex); return -1; } } /// <summary> /// Whether the specified object exists /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns>-1:Exception, 1: Existed, 0: Not Exist</returns> public int IsExists<T>(T entity) where T : class { try { using (DataAccessorObjectEntities context = new DataAccessorObjectEntities()) { object value; var entityKeyValues = new List<KeyValuePair<string, object>>(); EntitySet entitySet = context.CreateObjectSet<T>().EntitySet; foreach (var member in entitySet.ElementType.KeyMembers) { var info = entity.GetType().GetProperty(member.Name); var tempValue = info.GetValue(entity, null); var pair = new KeyValuePair<string, object>(member.Name, tempValue); entityKeyValues.Add(pair); } var key = new EntityKey(entitySet.EntityContainer.Name + "." + entitySet.Name, entityKeyValues); if (context.TryGetObjectByKey(key, out value)) { return value != null ? 1 : 0; } } } catch (Exception ex) { PrintExceptionLog(ex); } return -1; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="selector"></param> /// <param name="maxfunc"></param> /// <returns></returns> public object Max<T>(Func<T, bool> selector, Func<T, object> maxfunc) where T : class { object result = null; try { using (DataAccessorEntities context = new DataAccessorEntities()) { result = context.Set<T>().Where(selector).Max(maxfunc); } } catch (Exception ex) { PrintExceptionLog(ex); } return result; } #endregion #region Private Method /// <summary> /// Print Exception Log Info /// </summary> /// <param name="ex"></param> private void PrintExceptionLog(Exception ex) { StackTrace trace = new StackTrace(); MethodBase method = trace.GetFrame(1).GetMethod(); LogHelper.Instance.Error(string.Format("{0}::{1} - Exception:", formatString, method), ex); } #endregion}
}
0 0
- EntityFramework DB操作帮助类
- PHP DB操作类
- DB操作类
- JsonHelper操作帮助类
- Excel操作帮助类
- JsonHelper 操作帮助类
- lua db操作工具类
- DB操作
- vs2013连接mysql数据库并使用EntityFramework技术db first
- EntityFrameWork
- jdbc操作数据库帮助类
- C#xml操作帮助类
- Http连接操作帮助类
- 数据库操作的一般类:DB.Manager
- PHP操作mssql的db类
- 操作数据库的一个DB类
- 常用连接操作数据库的DB类
- discuz X3.2 DB:类数据库操作
- 从服务器下载&&OTA升级&&JSON文件解析
- 脚本语言学习
- android design
- jni基本介绍
- Net::POP3, login(), list(), get()
- EntityFramework DB操作帮助类
- Android学习系列(14)--App列表之圆角ListView
- Python创建和连接SqLite数据库
- Java加密和解密
- Redis 集合(Set)
- java 获取域名
- C#做窗体应用程序时,对excel涉及的背景色设置,边框设置,数据如何导入等的实现
- Socket编程(六)---TCP/NIO实例讲解
- 一句话介绍Hadoop家族产品