public IActionResult Post([FromBody] User value) { _context.Users.Add(value); _context.SaveChanges(); return(NoContent()); //return CreatedAtRoute("User", new { id = value.Id }, value); }
public UsersController(testingContext context) { _context = context; if (_context.Users.Count() == 0) { _context.Users.Add(new User { Username = "******", Id = 900, Email = "*****@*****.**" }); _context.SaveChanges(); } }
public IActionResult CreateExpandedCharacter(string characterName, int level = 1, int classId = 1) { using (var context = new testingContext()) { using (var transaction = context.Database.BeginTransaction()) { try { if (context.Character.Any(c => c.Name.Equals(characterName, StringComparison.OrdinalIgnoreCase)) || characterName.Trim().Equals(string.Empty)) { transaction.Rollback(); return(BadRequest("This character name is already taken.")); } // Use ToString here to ignore coercion failures between Class.ClassId and this.classId if (!context.Class.Any(c => c.ClassId.ToString().Equals(classId.ToString()))) { transaction.Rollback(); return(BadRequest("This character class does not exist.")); } context.Character.Add(new Character() { Name = characterName, ClassId = classId, Level = level, Strength = 1, Agility = 1, Vitality = 1, Intellegence = 1, Dexterity = 1, Experience = 1 }); context.SaveChanges(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); return(BadRequest($"Dont do this: {Environment.NewLine}{ex.StackTrace}")); } } return(Content($"[{characterName}] has been created as a [{context.Class.Find(classId).ClassName}] at level [{level}]")); } }
static void Main(string[] args) { using testingContext Context = new testingContext(); //Delete var test = Context.Products .Where(p => p.Name == "Test").FirstOrDefault(); if (test is Product) { Context.Remove(test); } Context.SaveChanges(); //--- ////Update //var test = Context.Products // .Where(p => p.Name == "Test").FirstOrDefault(); //if (test is Product) //{ // test.Price = 7.99m; //} //Context.SaveChanges(); ////----- //Select var products = Context.Products .Where(p => p.Price >= 6.5m) .OrderBy(p => p.Name); foreach (Product product in products) { Console.WriteLine($"Id:{product.Id}"); Console.WriteLine($"Name:{product.Name}"); Console.WriteLine($"Price:{product.Price}"); Console.WriteLine(new string('-', 20)); } //----- //Post TestCustomer customer = new TestCustomer() { FirstName = "Test", LastName = "h", CVR = "" }; Context.Add(customer); Product Ball = new Product() { Name = "ball", Price = 5.4M }; Context.Add(Ball); PrivateCustomer privateCustomer = new PrivateCustomer() { FirstName = "PrivateTest", LastName = "Z", CPR = "" }; Context.SaveChanges(); //---- }