public static void Initialize(MarketContext context) { context.Database.EnsureCreated(); if (context.Set <ProductEntity>().Any()) { return; // DB has been seeded } var products = new ProductEntity[] { new ProductEntity { Code = "39533028", Name = "Elcykel Allegro", Description = "En smart och tillförlitlig elcykel för dam från Batavus utmärkt både för långa och kortare turer.", Category = "Sport", Price = 18499 / 1.125 }, new ProductEntity { Code = "40266837", Name = "Lapierre Overvolt Urban 300", Description = "Praktisk och bekväm elcykel med upprätt körställning.", Category = "Sport", Price = 20990 / 1.125 }, new ProductEntity { Code = "p35372817", Name = "Chrome cast 2nd generationen", Description = "Visar film eller annan media från mobilen på TV:n", Category = "Hemelektronik", Price = 390 / 1.125 }, new ProductEntity { Code = "35552289", Name = "Apple TV 64GB 4th generation", Description = "Nu kommer App Store till din tv. Du kan förvänta dig massor av spännande spel.", Category = "Hemelektronik", Price = 1990 / 1.125 }, new ProductEntity { Code = "40151785", Name = "Big Foot truck 2wd", Description = "Det här är bilen som startade alltihop och skapade den idag enorma monster-truck trenden.", Category = "Leksaker", Price = 2795 / 1.125 } }; foreach (var prod in products) { context.Set <ProductEntity>().Add(prod); } context.SaveChanges(); }
public virtual ActionResult Delete(long id) { try { using (var db = new MarketContext()) { var models = db.Set <T>(); var model = models.Find(id); if (model == null) { throw new Exception("رکورد با ایدی داده شد یافت نشد"); } db.Entry(model).State = EntityState.Deleted; db.SaveChanges(); return(RedirectToAction("Index")); } return(RedirectToAction("Images")); } catch (Exception e) { Console.WriteLine(e); return(PartialView("Error", e)); } }
public virtual T GetByID(ID id) { try { dbContext = new MarketContext(); return(dbContext.Set <T>().Find(id)); } catch (Exception ex) { throw ex; } }
protected internal static MarketContext dbContext; //bulunduğu projeden erişilir.Bulunduğu projeden kalıtım alırsa kullanabilir. public virtual List <T> GetAll() { try { dbContext = new MarketContext(); return(dbContext.Set <T>().ToList()); } catch (Exception ex) { throw ex; } }
public virtual void Delete(T entity) { try { dbContext = dbContext ?? new MarketContext(); dbContext.Set <T>().Remove(entity); dbContext.SaveChanges(); } catch (Exception ex) { throw ex; } }
public virtual void Insert(T entity) //T model { try { dbContext = dbContext ?? new MarketContext(); dbContext.Set <T>().Add(entity); dbContext.SaveChanges(); } catch (Exception ex) { throw ex; } }
public ActionResult Index(int skip = 0, int count = 20) { using (var db = new MarketContext()) { var records = db.Set <T>().AsQueryable(); if (skip > 0) { records = records.Skip(skip); } ViewBag.total = records.Count(); ViewBag.page = skip; ViewBag.action = "Index"; return(View(records.Take(count).ToList())); } }
public ActionResult Create(long id = 0) { if (id != 0) { using (var db = new MarketContext()) { var models = db.Set <T>(); var model = models.Find(id); if (model == null) { throw new Exception("رکورد با ایدی داده شد یافت نشد"); } return(View(model)); } } return(View()); }
public ActionResult Create(T product) { try { if (!ModelState.IsValid) { throw new Exception(ModelState.ModelStateToString()); } using (var db = new MarketContext()) { var models = db.Set <T>(); if (product.Id == 0) { models.Add(product); } else { var exist = models.Find(product.Id); if (exist == null) { throw new Exception("محصول مورد نظر با کد داده شده یافت نشد"); } db.Entry(exist).CurrentValues.SetValues(product); } db.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception e) { Console.WriteLine(e); return(PartialView("Error", e)); } }
public virtual async Task <ActionResult> UploadImage(long id) { try { using (var db = new MarketContext()) { if (Request.Files.Count == 0 && string.IsNullOrEmpty(Request.Form["FileFromNet"])) { throw new Exception("هیچ فایلی ارسال نشده است"); } var models = db.Set <T>(); var model = models.Find(id); if (model == null) { throw new Exception("رکورد با ایدی داده شد یافت نشد"); } foreach (HttpPostedFileBase fileBase in Request.Files) { byte[] fileData = null; using (var binaryReader = new BinaryReader(fileBase.InputStream)) { fileData = binaryReader.ReadBytes(fileBase.ContentLength); } var img = new Image { Source = fileData, FileName = fileBase.FileName, ContentType = fileBase.ContentType, }; SetImageRefId(ref img, id); db.Images.Add(img); } if (!string.IsNullOrEmpty(Request.Form["FileFromNet"])) { var url = Request.Form["FileFromNet"]; var byteArr = LoadImageFromWeb(url); var img = new Image { Source = await byteArr, FileName = url, ContentType = "image/jpeg", }; SetImageRefId(ref img, id); db.Images.Add(img); } db.SaveChanges(); return(RedirectToAction("Images", new { id = id })); } } catch (Exception e) { Console.WriteLine(e); return(PartialView("Error", e)); } }
public Repository(MarketContext dbContext) { _dbContext = dbContext; _entities = _dbContext.Set <T>(); }
public void Create(TEntity item) { var entity = db.Set <TEntity>().Add(item); }