public void AddorUpdate(ResourceDto dto) { var entity = dto.MapTo<Resource>(); //var query = _resourceRepository.GetAllAsNoTracking(); //var model = query.SingleOrDefault(p => p.Id == entity.Id); var model = _resourceRepository.Find(entity.Id); Resource resourceParent = entity.ParentId == null ? null : _resourceRepository.Find((Guid)entity.ParentId); if (model == null) { entity.FixPathAndLevel(resourceParent); _resourceRepository.Add(entity); } else { model.FixPathAndLevel(resourceParent); //model.Id = entity.Id; model.ApplicationId = entity.ApplicationId; model.ParentId = entity.ParentId; model.Name = entity.Name; //model.Path = entity.Path; //model.Level = entity.Level; model.SortId = entity.SortId; model.Uri = entity.Uri; model.Type = entity.Type; model.Enabled = entity.Enabled; model.Version = entity.Version; _resourceRepository.Update(model); } }
/// <summary> /// 转换为资源数据传输对象 /// </summary> /// <param name="entity">资源实体</param> /// <param name="roleId">角色编号</param> public static ResourceDto ToDto(this Resource entity, Guid roleId) { ResourceDto dto = entity.MapTo <ResourceDto>(); dto.Checked = entity.Permissions.Any(p => p.RoleId == roleId); return(dto); }