using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; using VOL.Core.BaseProvider; using VOL.Core.EFDbContext; namespace VOL.Core.Extensions { public static class DbContextExtension { public static TFind FindById(this BaseDbContext dbContext, object id) where TFind : class { return dbContext.QueryOriginListByKeys([id],false)?.FirstOrDefault(); } public static async Task FindByIdAsync(this BaseDbContext dbContext, object id) where TFind : class { return (await dbContext.QueryOriginListByKeysAsync([id],false))?.FirstOrDefault(); } public static int Update(this BaseDbContext dbContext, TSource entity, string[] properties, bool saveChanges = false) where TSource : class { return dbContext.UpdateRange(new List() { entity }, properties, saveChanges); } public static int Update(this BaseDbContext dbContext, TSource entity, bool saveChanges = false) where TSource : class { return dbContext.UpdateRange(new List() { entity }, new string[0], saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, Expression> properties, bool saveChanges = false) where TSource : class { return dbContext.UpdateRange(entities, properties?.GetExpressionProperty(), saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, bool saveChanges = false) where TSource : class { return dbContext.UpdateRange(entities, new string[0], saveChanges); } public static int UpdateRange(this BaseDbContext dbContext, IEnumerable entities, string[] properties, bool saveChanges = false) where TSource : class { if (properties != null && properties.Length > 0) { PropertyInfo[] entityProperty = typeof(TSource).GetProperties() .Where(x => x.GetCustomAttribute() == null).ToArray(); string keyName = entityProperty.GetKeyName(); if (properties.Contains(keyName)) { properties = properties.Where(x => x != keyName).ToArray(); } properties = properties.Where(x => entityProperty.Select(s => s.Name).Contains(x)).ToArray(); } foreach (TSource item in entities) { if (properties == null || properties.Length == 0) { dbContext.Entry(item).State = EntityState.Modified; continue; } var entry = dbContext.Entry(item); properties.ToList().ForEach(x => { entry.Property(x).IsModified = true; }); } if (!saveChanges) { return 0; } else { dbContext.SaveChanges(); } return entities.Count(); } /// /// 通过主键批量删除 /// /// 主键key /// 是否连明细一起删除 /// public static int DeleteWithKeys(this BaseDbContext dbContext, object[] keys, bool saveChange = false) where T : class { var keyPro = typeof(T).GetKeyProperty(); foreach (var key in keys.Distinct()) { T entity = Activator.CreateInstance(); keyPro.SetValue(entity, key.ChangeType(keyPro.PropertyType)); dbContext.Entry(entity).State = EntityState.Deleted; } if (saveChange) { dbContext.SaveChanges(); } return keys.Length; } public static int Delete(this BaseDbContext dbContext, [NotNull] Expression> wheres, bool saveChange = false) where T : class { var keyProperty = typeof(T).GetKeyProperty(); string keyName = typeof(T).GetKeyProperty().Name; var expression = keyName.GetExpression(); var ids = dbContext.Set().Where(wheres).Select(expression).ToList(); List list = new List(); foreach (var id in ids) { T entity = Activator.CreateInstance(); keyProperty.SetValue(entity, id); list.Add(entity); } dbContext.RemoveRange(list); if (saveChange) { return dbContext.SaveChanges(); } return 0; } /// /// 过滤逻辑删除 /// /// /// /// public static IQueryable FilterLogicDel(this IQueryable query) where T : class { return query; } } }