public void Add(MobilesVM mob) { var data = mapper.Map <Mobiles>(mob); //var path = "/wwwroot/Files/Photos/"; data.ImageName = UploadeFileHelper.SaveFile(mob.ImageUrl, "Mobiles/"); db.Mobiles.Add(data); db.SaveChanges(); }
public ActionResult Add(Customer customer) { if (customer != null) { _container.Add(customer); _container.SaveChanges(); TempData["Success"] = "Add New Customer Successed!"; return(RedirectToAction("Index")); } else { TempData["Error"] = "Form Data Required!"; return(View()); } }
private static long LoadFolders(DbContainer container, DirectoryInfo origin, int?parentId = null) { var sw = new Stopwatch(); sw.Start(); var originFolder = container.FolderSet.FirstOrDefault(f => f.Name == origin.Name && f.ParentId == parentId); if (originFolder == null) { originFolder = new Folder() { Name = origin.Name, ParentId = parentId }; container.Add(originFolder); container.SaveChanges(); } var dirs = origin.EnumerateDirectories(); foreach (var folder in dirs) { LoadFolders(container, folder, originFolder.Id); } sw.Stop(); return(sw.ElapsedMilliseconds); }
public int Create(SysLog entity) { using (DbContainer db = new DbContainer()) { db.SysLog.Add(entity); return(db.SaveChanges()); } }
public int Create(SysSample entity) { using (DbContainer db = new DbContainer()) { db.SysSample.Add(entity); return db.SaveChanges(); } }
public void Delete(DbContainer db, string[] deleteCollection) { var collection = (from f in db.SysLog where deleteCollection.Contains(f.ID) select f).ToList(); collection.ForEach(item => { db.Entry(item).State = System.Data.Entity.EntityState.Deleted; db.SysLog.Remove(item); }); db.SaveChanges(); }
public int Delete(int id) { using (DbContainer db = new DbContainer()) { var entity = GetById(id); if (entity != null) { //改变实体状态,否则无法删除(报错) db.Entry(entity).State = EntityState.Deleted; db.Set<SysSample>().Remove(entity); } return db.SaveChanges(); } }
public bool Delete(string id) { using (DbContainer db = new DbContainer()) { var entity = GetById(id); if (entity != null) { db.Entry(entity).State = System.Data.Entity.EntityState.Deleted; db.SysException.Remove(entity); return(db.SaveChanges() > 0); } return(false); } }
public int Edit(SysSample entity) { try { using (DbContainer db = new DbContainer()) { db.Set<SysSample>().Attach(entity); db.Entry(entity).State = EntityState.Modified; return db.SaveChanges(); } } catch (Exception ex) { return 0; } }
/// <summary> /// 写入异常 数据库,文本 /// </summary> /// <param name="ex"></param> public static void WriteException(Exception ex) { try { using (DbContainer db = new DbContainer()) { SysException model = new SysException() { ID = ResultHelper.NewId, HelpLink = ex.HelpLink, Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, TargetSite = ex.TargetSite.ToString(), Data = ex.Data.ToString(), CreateTime = ResultHelper.NowTime }; db.SysException.Add(model); db.SaveChanges(); } } catch (Exception ep) { try { //异常失败写入txt string path = @"~/exceptionLog.txt"; string txtPath = System.Web.HttpContext.Current.Server.MapPath(path);//获取绝对路径 using (StreamWriter sw = new StreamWriter(txtPath, true, Encoding.Default)) { sw.WriteLine((ex.Message + "|" + ex.StackTrace + "|" + ep.Message + "|" + DateTime.Now.ToString()).ToString()); sw.Dispose(); sw.Close(); } return; } catch { return; } } }
public bool PermanentlyDelete(UserMessage record) { var userMessage = Container.Table_Message.Remove(Container.Table_Message.Find(record.Id)); return(DbContainer.SaveChanges() == 1); }
public virtual bool Create(T model) { db.Set <T>().Add(model); return(db.SaveChanges() > 0); }
private static long LoadFiles(DbContainer container, DirectoryInfo origin, bool update, int?parentId = null) { Console.WriteLine($"Loading Files on folder [{origin.FullName}]."); var sw = new Stopwatch(); sw.Start(); var originFolder = container.FolderSet.FirstOrDefault(f => f.Name == origin.Name && f.ParentId == parentId); if (originFolder != null) { var added = 0; var updated = 0; foreach (var folderFile in origin.EnumerateFiles()) { Console.WriteLine($"[ONGOING] {folderFile.FullName} executionTime: {sw.ElapsedMilliseconds} "); var type = FileTypes.Find(type => type.Name == folderFile.Extension[1..].ToUpper()); if (type == null) { continue; } var file = container.FileSet.FirstOrDefault(f => f.Fullpath == folderFile.FullName); if (file == null) { file = GetFile(folderFile, originFolder.Id, type.Id); container.Add(file); container.SaveChanges(); } var photo = container.PhotoSet.FirstOrDefault(f => f.FileId == file.Id); if (photo == null) { added++; photo = photos.Load(file.Fullpath); if (photo != null) { if (type.Name != FileTypeEnum.CR2.ToString()) { var image = Image.FromFile(file.Fullpath); var thumb = image.Width > 0 && image.Height > 0 ? image.GetThumbnailImage(image.Width / Constants.ImageProperties.ThumbMultiplier, image.Height / Constants.ImageProperties.ThumbMultiplier, () => false, IntPtr.Zero) : image.GetThumbnailImage(Constants.ImageProperties.ThumbWidth, Constants.ImageProperties.ThumbHeight, () => false, IntPtr.Zero); photo.Thumbnail = new ImageConverter().ConvertTo(thumb, typeof(byte[])) as byte[]; image.Dispose(); thumb.Dispose(); } photoRep.Add(photo); container.SaveChanges(); } } else { var newPhoto = photos.Load(file.Fullpath); photo ??= new Photo(); photo.Iso = newPhoto.Iso; photo.Height = newPhoto.Height; photo.Width = newPhoto.Width; photo.FocalLength = newPhoto.FocalLength; photo.FStop = newPhoto.FStop; photo.DateTaken = newPhoto.DateTaken; photoRep.Update(photo); container.SaveChanges(); updated++; } } container.SaveChanges(); Console.WriteLine($"Added: {added}; updated/skipped: {updated}; executionTime: {sw.ElapsedMilliseconds} "); var dirs = origin.EnumerateDirectories(); foreach (var folder in dirs) { LoadFiles(container, folder, true, originFolder.Id); } } sw.Stop(); return(sw.ElapsedMilliseconds); }