public TRemote AddRecord <TLocal, TRemote>(TLocal local, Action <TLocal> BeforeNew = null /*, Action<TRemote> AfterNew = null*/) where TLocal : _BaseTableMain where TRemote : _BaseTableMain { BeforeNew?.Invoke(local); Pinhua2Helper.CompleteMainOnCreate(local); var remote = _mapper.Map <TLocal, TRemote>(local); _pinhua2Context.Entry <TRemote>(remote).State = EntityState.Added; //AfterNew?.Invoke(remote); //var i = _pinhua2Context.SaveChanges(); return(remote); }
public static TRemote funcEditRecord <TLocal, TRemote>(this Pinhua2Context context, TLocal local, Action <TLocal> BeforeNew = null /*, Action<TRemote> AfterNew = null*/) where TLocal : _BaseTableMain where TRemote : _BaseTableMain { var remote = context.Set <TRemote>().FirstOrDefault(m => m.RecordId == local.RecordId); if (remote == null) { return(null); } BeforeNew?.Invoke(local); //Pinhua2Helper.CompleteMainOnEdit(local); local.CreateTime = remote.CreateTime; local.CreateUser = remote.CreateUser; local.LastEditTime = DateTime.Now; local.LastEditUser = local.LastEditUser ?? "张凯译"; Mapper.Map <TLocal, TRemote>(local, remote); context.Entry <TRemote>(remote).State = EntityState.Modified; //AfterNew?.Invoke(remote); //context.SaveChanges(); return(remote); }
public static bool TryRecordDetailsAdd <TSrc, TSrcD, TDst, TDstD>(this Pinhua2Context context, TSrc src, IEnumerable <TSrcD> srcDSet, Action <TSrcD> Adding = null) where TSrc : _BaseTableMain where TSrcD : _BaseTableDetail where TDst : _BaseTableMain where TDstD : _BaseTableDetail { if (!srcDSet.Any()) { // 如果明细为空,直接返回 true ,避免主表数据无法保存成功 return(true); } foreach (var srcD in srcDSet) { Adding?.Invoke(srcD); srcD.RecordId = src.RecordId; var dstD = StaticAutoMapper.Current.Map <TSrcD, TDstD>(srcD); context.Entry <TDstD>(dstD).State = EntityState.Added; } var nRet = context.SaveChanges(); if (nRet > 0) { return(true); } else { return(false); } }
public static bool TryRecordAdd <TSrc, TDst>(this Pinhua2Context context, TSrc src, Action <TSrc> Adding = null) where TSrc : _BaseTableMain where TDst : _BaseTableMain { Adding?.Invoke(src); src.CreateTime = DateTime.Now; src.CreateUser = src.CreateUser ?? "张凯译"; var dst = StaticAutoMapper.Current.Map <TSrc, TDst>(src); context.Entry <TDst>(dst).State = EntityState.Added; var nret = context.SaveChanges(); if (nret > 0) { // SaveChanges 成功的话,把数据库中的数据比如 RecordId 返回 // src.RecordId = dst.RecordId; StaticAutoMapper.Current.Map <TDst, TSrc>(dst, src); return(true); } else { return(false); } }
public static bool TryRecordEdit <TSrc, TDst>(this Pinhua2Context context, TSrc src, /*out TDst outDst,*/ Action <TSrc> Editing = null) where TSrc : _BaseTableMain where TDst : _BaseTableMain { var dst = context.Set <TDst>().FirstOrDefault(m => m.RecordId == src.RecordId); if (dst == null) { //outDst = null; return(false); } Editing?.Invoke(src); src.CreateTime = dst.CreateTime; src.CreateUser = dst.CreateUser; src.LastEditTime = DateTime.Now; src.LastEditUser = src.LastEditUser ?? "张凯译"; StaticAutoMapper.Current.Map <TSrc, TDst>(src, dst); context.Entry <TDst>(dst).State = EntityState.Modified; var ret = context.SaveChanges(); if (ret > 0) { //outDst = dst; return(true); } else { //outDst = null; return(false); } }
public static TDst RecordEdit <TSrc, TDst>(this Pinhua2Context context, TSrc src, Action <TSrc> Editing = null /*, Action<TRemote> AfterNew = null*/) where TSrc : _BaseTableMain where TDst : _BaseTableMain { var dst = context.Set <TDst>().FirstOrDefault(m => m.RecordId == src.RecordId); if (dst == null) { return(null); } Editing?.Invoke(src); src.CreateTime = dst.CreateTime; src.CreateUser = dst.CreateUser; src.LastEditTime = DateTime.Now; src.LastEditUser = src.LastEditUser ?? "张凯译"; StaticAutoMapper.Current.Map <TSrc, TDst>(src, dst); context.Entry <TDst>(dst).State = EntityState.Modified; context.SaveChanges(); return(dst); }
public static TOriginalD RecordDetailAdd <TDtoD, TOriginalD>(this Pinhua2Context context, TDtoD dto, Action <TDtoD> Adding = null) where TDtoD : _BaseTableDetail where TOriginalD : _BaseTableDetail { Adding?.Invoke(dto); var remoteD = StaticAutoMapper.Current.Map <TDtoD, TOriginalD>(dto); context.Entry <TOriginalD>(remoteD).State = EntityState.Added; //context.SaveChanges(); return(remoteD); }
public static TOriginal RecordAdd <TDto, TOriginal>(this Pinhua2Context context, TDto local, Action <TDto> Adding = null /*, Action<TRemote> AfterNew = null*/) where TDto : _BaseTableMain where TOriginal : _BaseTableMain { Adding?.Invoke(local); Pinhua2Helper.CompleteMainOnCreate(local); var remote = StaticAutoMapper.Current.Map <TDto, TOriginal>(local); context.Entry <TOriginal>(remote).State = EntityState.Added; //AfterNew?.Invoke(remote); //context.SaveChanges(); return(remote); }
public static TOriginalD RecordDetailAdd <TDto, TDtoD, TOriginal, TOriginalD>(this Pinhua2Context context, TOriginal remote, TDtoD localD, Action <TDtoD> BeforeNewD = null /*, Action<TRemoteD> AfterNew = null*/) where TDto : _BaseTableMain where TDtoD : _BaseTableDetail where TOriginal : _BaseTableMain where TOriginalD : _BaseTableDetail { BeforeNewD?.Invoke(localD); //Pinhua2Helper.CompleteDetailOnCreate(remote, localD); localD.RecordId = remote.RecordId; var remoteD = StaticAutoMapper.Current.Map <TDtoD, TOriginalD>(localD); context.Entry <TOriginalD>(remoteD).State = EntityState.Added; //AfterNew?.Invoke(remoteD); //context.SaveChanges(); return(remoteD); }
public static TRemoteD funcNewDetail <TLocal, TLocalD, TRemote, TRemoteD>(this Pinhua2Context context, TRemote remote, TLocalD localD, Action <TLocalD> BeforeNewD = null /*, Action<TRemoteD> AfterNew = null*/) where TLocal : _BaseTableMain where TLocalD : _BaseTableDetail where TRemote : _BaseTableMain where TRemoteD : _BaseTableDetail { BeforeNewD?.Invoke(localD); //Pinhua2Helper.CompleteDetailOnCreate(remote, localD); localD.RecordId = remote.RecordId; var remoteD = Mapper.Map <TLocalD, TRemoteD>(localD); context.Entry <TRemoteD>(remoteD).State = EntityState.Added; //AfterNew?.Invoke(remoteD); //context.SaveChanges(); return(remoteD); }