public IActionResult CreateNewCharacter(string characterName) { using (var context = new testingContext()) { using (var contextTransaction = context.Database.BeginTransaction()) { var command = context.Database.GetDbConnection().CreateCommand(); command.CommandText = "Create_New_Character"; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new MySqlParameter("@CharacterName", MySqlDbType.VarChar) { Value = characterName }); try { command.ExecuteNonQuery(); contextTransaction.Commit(); return(Ok($"{characterName} has been created!")); } catch (Exception) { contextTransaction.Rollback(); return(BadRequest($"There was an error creating {characterName}.")); } } } }
static void Main(string[] args) { Console.WriteLine("MySql Users!"); var dbContext = new testingContext(); var users = dbContext.Users.ToList(); foreach (var c in users) { Console.WriteLine($"id {c.Id}, name {c.Username}, email {c.Email}"); } }
public UsersController(testingContext context) { _context = context; if (_context.Users.Count() == 0) { _context.Users.Add(new User { Username = "******", Id = 900, Email = "*****@*****.**" }); _context.SaveChanges(); } }
public IActionResult CharacterLookup(string characterName) { using (var context = new testingContext()) { if (string.IsNullOrEmpty(characterName.Trim())) { return(BadRequest(CharNotFound)); } return(Ok(JsonConvert.SerializeObject(context.Character .Where(x => x.Name.Contains(characterName, StringComparison.OrdinalIgnoreCase)) .Select(x => new { x.Name, x.Class.ClassName, x.Level }) .FirstOrDefault()))); } }
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(); //---- }
public Home(testingContext context) { _context = context; }