/// <summary> /// 保存资源。会从资源id和资源code两字段考虑是新增还是修改,无删除 /// </summary> /// <param name="resources"></param> public virtual void SaveResources(List <IResource> resources) { var allResources = _db.Set <TResource>().ToList(); resources.ForEach(resource => { var resourceKey = resource.GetKey(); var resourceEntity = allResources.FirstOrDefault(a => a.GetKey() == resourceKey || a.GetResourceCode() == resource.GetResourceCode()); if (resourceEntity == null) { //add var addDto = new TResource(); EasyMap.Map(resource.GetType(), typeof(TResource), resource, addDto, null); _db.Add(addDto); DbSetExtenssion.UpdateEntityCommonField(resourceEntity, EEntityOperType.Add, CurrentUserId, CurrentTenantId); } else { EasyMap.Map(resource.GetType(), typeof(TResource), resource, resourceEntity, null); DbSetExtenssion.UpdateEntityCommonField(resourceEntity, EEntityOperType.Update, CurrentUserId, CurrentTenantId); } }); _db.SaveChanges(); _memoryCache.Remove(resourceCacheKey); }
public static TEntity AddOrUpdate <TEntity, TDto, TKey>(this DbSet <TEntity> entities, TDto dto, TKey userId, TKey tenantId = default, List <TEntity> existEntities = null) where TEntity : class, IIdField <TKey>, new() where TDto : class, IIdField <TKey> { return(AddOrUpdateInternal(entities, dto, sourceDto => EasyMap.MapToNew <TEntity>(sourceDto), (sourceDto, entityDto) => EasyMap.Map(sourceDto.GetType(), entityDto.GetType(), sourceDto, entityDto, null), userId, tenantId, existEntities)); }
/// <summary> /// 实体列表的差异更新,包含增加、删除、更新 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <typeparam name="TDto"></typeparam> /// <typeparam name="TKey"></typeparam> /// <param name="entities"></param> /// <param name="existsEntities"></param> /// <param name="dtos"></param> /// <param name="userId"></param> /// <param name="tenantId"></param> public static void AddOrUpdateList <TEntity, TDto, TKey>(this DbSet <TEntity> entities, List <TEntity> existsEntities, List <TDto> dtos, TKey userId, TKey tenantId = default) where TEntity : class, IIdField <TKey>, new() where TDto : class, IIdField <TKey> { AddOrUpdateListInternal(entities, existsEntities, dtos, sourceDto => EasyMap.MapToNew <TEntity>(sourceDto), (sourceDto, entityDto) => EasyMap.Map(sourceDto.GetType(), entityDto.GetType(), sourceDto, entityDto, null), userId, tenantId); }
public void MapTest() { try { var sourceType = typeof(Source); var targetType = typeof(Target); var includeFields = new List <string> { "Name", "Age", "Birth", "Week", "ExceptField" }; Expression <Func <Target, object> > includeSelector = t => new { t.Name, t.Age, t.Birth, t.Week }; var exceptFields = new List <string> { "ExceptField" }; Expression <Func <Target, object> > exceptSelector = t => new { t.ExceptField }; var source = new Source { Name = "zhoujing", Age = 30, Birth = new DateTime(1989, 1, 1), NoMatch1 = "未匹配的字段,不会赋值到target", Week = DayOfWeek.Wednesday, ExceptField = "这个是排除的字段", NotIncludeField = "这个是不包含字段" }; var target = new Target(); target.Clear(); EasyMap.Map(sourceType, targetType, source, target, includeFields); Assert.True(IsMatch(source, target, false, true)); target.Clear(); EasyMap.Map(source, target); Assert.True(IsMatch(source, target, false, false)); target.Clear(); EasyMap.Map(source, target, includeFields); Assert.True(IsMatch(source, target, false, true)); target.Clear(); EasyMap.Map(source, target, includeSelector, exceptSelector); Assert.True(IsMatch(source, target, true, true)); target.Clear(); EasyMap.Map(source, target, includeFields, exceptFields); Assert.True(IsMatch(source, target, true, true)); } catch (Exception ex) { var a = ex; } }