internal void Update(int id, viewT updatedEntity) { dbT existingRecord = _db.Set <dbT>().Find(id); if (existingRecord == null) { throw new EntityNotFoundException(id, typeof(dbT)); } existingRecord = updatedEntity.Adapt(existingRecord); existingRecord.DateModified = DateTime.UtcNow; _db.SaveChanges(); }
public async Task <IActionResult> AddToCart(int id) { var product = await _context.Products.FindAsync(id); CartItem cartItem = new CartItem { User = GetCurrentUserAsync().Result, Product = product, Quantity = 1, TimeAdded = DateTime.Now }; _context.Add <CartItem>(cartItem); _context.SaveChanges(); return(Redirect("/")); }
public IActionResult Create(ProductViewModel model) { var img = model.Image; string uniqueFileName = null; if (img != null) { uniqueFileName = GetUniqueFileName(img.FileName); var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads"); var filePath = Path.Combine(uploads, uniqueFileName); img.CopyTo(new FileStream(filePath, FileMode.Create)); } var category = _context.Categories.Find(model.CategoryId); var product = new Product { ProductName = model.ProductName, Description = model.Description, ImageUrl = uniqueFileName, Price = model.Price, Category = category }; _context.Add <Product>(product); _context.SaveChanges(); return(RedirectToAction("Index", "Home")); }
static void Main(string[] args) { var file = "mydb.db"; File.Delete(file); Func <SqliteConnectionStringBuilder> getCon = () => new SqliteConnectionStringBuilder { ["Filename"] = file }; var con = getCon(); con.Mode = SqliteOpenMode.ReadWriteCreate; using (var db = new EfDataContext(con.ConnectionString)) { db.Database.Migrate(); } var con1 = getCon(); con1.Mode = SqliteOpenMode.ReadOnly; using (var db1 = new EfDataContext(con1.ConnectionString)) { // db1.Database.BeginTransaction(); var dogs = db1.Dogs.ToList(); var con2 = getCon(); con2.Mode = SqliteOpenMode.ReadWrite; using (var db2 = new EfDataContext(con2.ConnectionString)) { db2.Database.BeginTransaction(); var dog1 = new Dog { Name = "Minus" }; db2.Dogs.Add(dog1); db2.SaveChanges(); db2.Database.CommitTransaction(); } } }
public ServiceFeedback AddBlog(Blog model) { using (var context = new EfDataContext()) { if (model.ID == new Guid()) { model.ID = Guid.NewGuid(); } if (model.CreationDate == new DateTime()) { model.CreationDate = DateTime.Now; } context.Blogs.Add(model); context.SaveChanges(); } return(new ServiceFeedback { Message = "Success", Status = 200 }); }
public virtual void Add(TEntity obj) { _Db.Set <TEntity>().Add(obj); _Db.SaveChanges(); }