public ActionResult Add(Actor actor) { //dohvaćanje idućeg slobodnog ID-a, može li bolje/izravnije? actor.ActorId = (ushort)((_context.Actor.Select(x => x.ActorId).Max()) + 1); _context.Add(actor); _context.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult updateStatus(int TaskId, int statusId) { Task tsk; tsk = _context.Task.Where(x => x.Id == TaskId).FirstOrDefault(); tsk.Statusid = statusId; _context.Task.Update(tsk); _context.SaveChanges(); return(Ok(tsk)); }
public void UpdateActorInfo(int actorId, string bio, string firstName, string lastName, string socialMediaURL) { using (var db = new sakilaContext()) { var actorToUpdate = db.Actor.Where(a => a.ActorId == actorId).FirstOrDefault(); if (actorToUpdate != null) { actorToUpdate.FirstName = firstName; actorToUpdate.LastName = lastName; actorToUpdate.LastUpdate = DateTimeOffset.Now; } var socialMedia = db.Socialmedia.Where(s => s.ActorId == actorId).FirstOrDefault(); if (socialMedia != null) { socialMedia.Url = socialMediaURL; } else if (socialMediaURL != string.Empty) { Socialmedia actorSocialMedia = new Socialmedia(); actorSocialMedia.ActorId = (short)actorId; actorSocialMedia.Url = socialMediaURL; db.Socialmedia.Add(actorSocialMedia); } db.SaveChanges(); } }
static void Main(string[] args) { var dbContext = new sakilaContext(); /* * var records = dbContext.Film.Include(f => f.FilmActor).ThenInclude(r => r.Actor).ToList(); * foreach (var record in records) * { * Console.WriteLine($"Film: {record.Title}"); * var counter = 1; * foreach (var fa in record.FilmActor) * { * Console.WriteLine($"\tActor {counter++}: {fa.Actor.FirstName} {fa.Actor.LastName}"); * } * } * * var city = new City() * { * CityId = 1001, Name = "Redmond", CountryId = 103 * }; * dbContext.Add(city); * dbContext.SaveChanges(); */ var uTarget = dbContext.City.SingleOrDefault(c => c.CityId == 1001); if (uTarget != null) { // uTarget.Name = "Kirkland"; // dbContext.Update(uTarget); dbContext.Remove(uTarget); dbContext.SaveChanges(); } }
public bool UpdateMenuItem(MenuItemToUpdateDTO menuItemToUpdateDTO) { try { using (var db = new sakilaContext()) { Menu menuToUpdate = db.Menu.Where(m => m.Menuid == menuItemToUpdateDTO.menuid).FirstOrDefault(); menuToUpdate.Description = menuItemToUpdateDTO.description; if (menuItemToUpdateDTO.imageid != null) { menuToUpdate.Imageid = (long)menuItemToUpdateDTO.imageid; } menuToUpdate.Menutype = menuItemToUpdateDTO.menutype; menuToUpdate.Price = menuItemToUpdateDTO.price; menuToUpdate.Title = menuItemToUpdateDTO.title; db.SaveChanges(); return(true); } } catch (Exception ex) { return(false); } }
public MenuItemDTO CreateMenuItem(MenuItemToCreateDTO menuItemToCreateDTO) { using (var db = new sakilaContext()) { Menu menuToCreate = new Menu(); menuToCreate.Description = menuItemToCreateDTO.description; menuToCreate.Menutype = menuItemToCreateDTO.menutype; menuToCreate.Price = menuItemToCreateDTO.price; menuToCreate.Title = menuItemToCreateDTO.title; if (menuItemToCreateDTO.imageid != null) { menuToCreate.Imageid = (long)menuItemToCreateDTO.imageid; } else { menuToCreate.Imageid = 1; } db.Menu.Add(menuToCreate); db.SaveChanges(); MenuItemDTO menuItemDTOToReturn = new MenuItemDTO(); menuItemDTOToReturn.description = menuToCreate.Description; menuItemDTOToReturn.imageUrl = db.Image.Where(i => i.Imageid == menuToCreate.Imageid).FirstOrDefault().ImageUrl; menuItemDTOToReturn.menuId = menuToCreate.Menuid; menuItemDTOToReturn.price = menuToCreate.Price; menuItemDTOToReturn.title = menuToCreate.Title; return(menuItemDTOToReturn); } }
private void deleteCity() { using (var dbContext = new sakilaContext()) { var city = dbContext.City.SingleOrDefault(x => x.CityId == 1001); if (city != null) { dbContext.Remove(city); dbContext.SaveChanges(); } } }
private void addCity() { using (var dbContext = new sakilaContext()) { var city = new City() { CityId = 1001, Name = "Alappuzha", CountryId = 103 }; dbContext.Add(city); dbContext.SaveChanges(); } }
private void updateCityRecord() { using (var dbContext = new sakilaContext()) { var city = dbContext.City.SingleOrDefault(x => x.CityId == 1001); if (city != null) { city.Name = "Alleppy"; dbContext.Update(city); dbContext.SaveChanges(); } } }
static void Main(string[] args) { // var dbContext = new sakilaContext(); // var actors = dbContext.Actor.ToList(); // foreach (var a in actors) { // System.Console.WriteLine($"ID:{a.ActorId} Name:{a.FirstName} {a.LastName}"); // } //reading joined tables // var dbContext = new sakilaContext(); // var records = dbContext.Film.Include(f => f.FilmActor).ThenInclude(r => r.Actor).ToList(); // foreach (var record in records) { // System.Console.WriteLine($"Film: {record.Title}"); // var counter = 1; // foreach (var fa in record.FilmActor) { // System.Console.WriteLine($"\tActor {counter++}: {fa.Actor.FirstName} {fa.Actor.LastName}"); // } // } //add a new city into the database // var dbContext = new sakilaContext(); // var city = new City() { CityId = 1001, City1 = "Redmond", CountryId = 103 }; // dbContext.Add(city); // dbContext.SaveChanges(); //update an existing record // var dbContext = new sakilaContext(); // var uTarget = dbContext.City.SingleOrDefault(c => c.CityId == 1001); // if (uTarget != null) { // uTarget.City1 = "Kirkland"; // change Name to City1 if you did not update the City class file // dbContext.Update(uTarget); // dbContext.SaveChanges(); // } //delete an existing record var dbContext = new sakilaContext(); var dTarget = dbContext.City.SingleOrDefault(c => c.CityId == 1001); if (dTarget != null) { dbContext.Remove(dTarget); dbContext.SaveChanges(); } }
static void Main(string[] args) { var dbContext = new sakilaContext(); // var actors = dbContext.Actor.ToList(); // foreach (var a in actors) // { // Console.WriteLine($"ID:{a.ActorId} First Name:{a.FirstName} Last Name: {a.LastName} Created on: {a.LastUpdate}"); // } // var records = dbContext.Film.Include(f => f.FilmActor).ThenInclude(r => r.Actor).ToList(); // foreach (var record in records) // { // Console.WriteLine($"Film: {record.Title}"); // var counter = 1; // foreach (var fa in record.FilmActor) // { // Console.WriteLine($"\tActor {counter++}: {fa.Actor.FirstName} {fa.Actor.LastName}"); // } // } // var city = new City() { CityId = 601, Name = "Redmond", CountryId = 103 }; // dbContext.Add(city); // dbContext.SaveChanges(); // var uTarget = dbContext.City.SingleOrDefault(c => c.CityId == 601); // if (uTarget != null) // { // uTarget.Name = "Kirkland"; // change Name to City1 if you did not update the City class file // dbContext.Update(uTarget); // dbContext.SaveChanges(); // } var dTarget = dbContext.City.SingleOrDefault(c => c.CityId == 601); if (dTarget != null) { dbContext.Remove(dTarget); dbContext.SaveChanges(); } }
public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); var dbContext = new sakilaContext(); // var records = dbContext.Film.Include(f => f.FilmActor).ThenInclude(r => r.Actor).ToList(); // foreach (var record in records) // { // System.Console.WriteLine($"Film: {record.Title}"); // var counter = 1; // foreach (var fa in record.FilmActor) // { // System.Console.WriteLine($"\tActor {counter++}: {fa.Actor.FirstName} {fa.Actor.LastName}"); // } // } // var city = new City() { City1 = "Redmond", CountryId = 103 }; // dbContext.Add(city); // dbContext.SaveChanges(); // var uTarget = dbContext.City.SingleOrDefault(c => c.CityId == 1001); // if (uTarget != null) // { // uTarget.City1 = "Kirkland"; // dbContext.Update(uTarget); // dbContext.SaveChanges(); // } var dTarget = dbContext.City.SingleOrDefault(c => c.CityId == 1001); if (dTarget != null) { dbContext.Remove(dTarget); dbContext.SaveChanges(); } }
public bool DeleteMenuItem(long menuid) { try { using (var db = new sakilaContext()) { Menu menuItemToDelete = db.Menu.Where(m => m.Menuid == menuid).FirstOrDefault(); if (menuItemToDelete == null) { return(false); } db.Menu.Remove(menuItemToDelete); db.SaveChanges(); return(true); } } catch (Exception ex) { return(false); } }