public ReturnSet <bool> AddPost(PostCreationRequestItem requestItem) { using (var eFactory = new EFModel()) { var post = eFactory.Posts.Create(); post.Active = true; post.Created = DateTime.Now; post.Modified = DateTime.Now; post.Body = requestItem.Body; post.Summary = requestItem.Body.Length > 255 ? requestItem.Body.Substring(0, 252) + "..." : requestItem.Body; post.Title = requestItem.Title; post.SafeURL = requestItem.Title.Replace(" ", "-"); eFactory.Posts.Add(post); eFactory.SaveChanges(); var key = eFactory.DGTPostKeys.Create(); key.PostID = post.ID; key.PostKey = $"{post.Created.Year}_{post.Created.Month}_{post.Created.Day}_{post.SafeURL}"; eFactory.DGTPostKeys.Add(key); eFactory.SaveChanges(); return(new ReturnSet <bool>(true)); } }
public virtual int Insert(TEntity entity, bool isSave = true) { EntityState state = _dbContext.Entry(entity).State; if (state == EntityState.Detached) { _dbContext.Entry(entity).State = EntityState.Added; } // var count=_dbContext.SaveChanges(); return(isSave ? _dbContext.SaveChanges() : 0); }
public ActionResult Create([Bind(Include = "AlbumId,Title,CategoryName,ArtistName,UnitPrice")] Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(album)); }
/// <summary> /// to insert a new object in db /// </summary> /// <param name="obj">a new object in db</param> public int Save(IDTO dto) { int id = 0; try { if (ValidateEntitie(dto)) { Client client = Mapper.Map <Client>(ValidateBusiness(dto)); using (var db = new EFModel()) { using (var transaction = db.Database.BeginTransaction()) { // Display all Clients from the database db.Client.Add(client); id = db.SaveChanges(); transaction.Commit(); } } } return(id); } catch (Exception ex) { throw new Exception(ex.Message); } }
public ActionResult Index(HttpPostedFileBase file) { if (file?.ContentLength > 0) { try { List <Moviment> Lmoviment = new List <Moviment>(); CultureInfo cultureInfo = new CultureInfo("fr-FR"); using (StreamReader str = new StreamReader(file.InputStream)) { string line; while ((line = str.ReadLine()) != null) { string[] data = line.Split(','); if (data[0] != "Date" && data[1] != "Price") { Moviment moviment = new Moviment { Date = DateTime.Parse(data[0], cultureInfo), Price = double.Parse(data[1]), }; Lmoviment.Add(moviment); } } } foreach (var item in Lmoviment) { ctx.Moviments.Add(item); ctx.SaveChanges(); } ViewBag.SUCCESS = "Success"; return(View()); } catch (Exception message) { ViewBag.ERROR = "failed to import the file"; return(View()); } } else { return(View()); } }
public int InsertData(int num) { Users entity = new Users(); entity.LoginName = Guid.NewGuid().ToString("N"); entity.Password = ""; entity.CreateTime = DateTime.Now; entity.UpdateTime = DateTime.Now; entity.Remark = num.ToString(); for (int i = 0; i < num; i++) { entity.Status = i % 2; model.Set <Users>().Add(entity); model.SaveChanges(); } return(num); }
/// <summary> /// to delete a user from database /// </summary> /// <param name="id">the id primary key from object</param> public void Delete(int codigo) { try { ClientDTO obj = new ClientDTO(); using (var db = new EFModel()) { // Display all Blogs from the database var query = from b in db.Client where b.codigo == codigo select b; Client client = query.FirstOrDefault(); //Delete it from memory db.Client.Remove(client); //Save to database\ db.SaveChanges(); } } catch (Exception ex) { throw new Exception(ex.Message); } }
/// <summary> /// to update an object in db /// </summary> /// <param name="obj">a new object in db</param> public int Update(IDTO dto) { int id = 0; try { if (ValidateEntitie(dto)) { Client client = Mapper.Map <Client>(ValidateBusiness(dto)); using (var db = new EFModel()) { // Display all Clients from the database db.Entry(client).State = System.Data.Entity.EntityState.Modified; id = db.SaveChanges(); } } return(id); } catch (Exception ex) { throw new Exception(ex.Message); } }