private static void UpdateBattle1SamuraiDiscScen() { Battle b; using (var db = new SamuraiDbContext()) { b = db.Battles.Single(b => b.Id == 3); } var samuraiBattles = new List <SamuraiBattle> { new SamuraiBattle { SamuraiId = 7, BattleId = 3 }, new SamuraiBattle { SamuraiId = 14, BattleId = 3 } }; using (var db2 = new SamuraiDbContext()) { db2.SamuraiBattle.AddRange(samuraiBattles); db2.SaveChanges(); } }
private static void SamuraiWithQoutes() { var samurai = new Samurai(); samurai.Quotes.Add(new Quote { Text = "Qoute1" }); samurai.Quotes.Add(new Quote { Text = "Qoute2" }); samurai.Quotes.Add(new Quote { Text = "Qoute3" }); samurai.Name = "Andrej"; samurai.SecretIdentity = new SecretIdentity { RealName = "Jacki" }; using (var db = new SamuraiDbContext()) { db.Samurais.Add(samurai); db.SaveChanges(); } }
//delete private static void DeleteWhileNotTracked() { var samurai = _context.Samurais.FirstOrDefault(s => s.Name == "Everton Mota"); using (var ContextApp = new SamuraiDbContext()) { ContextApp.Samurais.Remove(samurai); ContextApp.SaveChanges(); } }
private static void QueryAndUpdateSamurai_Disconnected() { var samurai = _context.Samurais.FirstOrDefault(s => s.Name == "Kikuchiyo"); samurai.Name += "San"; using (var newContextInstance = new SamuraiDbContext()) { newContextInstance.Samurais.Update(samurai); newContextInstance.SaveChanges(); } }
public static void AddQuote() { using (var db = new SamuraiDbContext()) { var s = db.Samurais.Single(x => x.Id == 11); s.Quotes.Add(new Quote { Text = "Quote 3" }); db.SaveChanges(); } }
private static void LinqQuery() { using (var dbNew = new SamuraiDbContext()) { var query = from s in dbNew.Samurais where EF.Functions.Like(s.Name, "S%") select s; var temp = query.ToList(); } }
public static void GetQuotes() { using (var db = new SamuraiDbContext()) { var quotes = db.Quotes.Include(q => q.Samurai).Where(q => EF.Functions.Like(q.Text, "Q%")).ToList(); foreach (var quote in quotes) { Console.WriteLine($"Samuri: {quote.Samurai.Name} - {quote.Text}"); } } }
public static void GetSamuraiQuote() { using (var db = new SamuraiDbContext()) { var result = db.Samurais.Select(s => new { SamName = s.Name, QuotesCount = s.Quotes.Count }).ToList(); foreach (var item in result) { Console.WriteLine($"{item.SamName} : Count: {item.QuotesCount}"); } } }
private static void DeleteWhileNotTracked() { var samurai = _context.Samurais.FirstOrDefault(s => s.Name == "Heihachi Hayashida"); using (var contextNewAppInstance = new SamuraiDbContext()) { contextNewAppInstance.Samurais.Remove(samurai); //contextNewAppInstance.Entry(samurai).State=EntityState.Deleted; contextNewAppInstance.SaveChanges(); } }
private static void QueryAndUpdateBattle_Disconnected() { var battle = _context.Battles.FirstOrDefault(); battle.EndDate = new DateTime(1560, 06, 30); using (var newContextInstance = new SamuraiDbContext()) { newContextInstance.Battles.Update(battle); newContextInstance.SaveChanges(); } }
private static void UpdateMultipleSamurai() { using (var db = new SamuraiDbContext()) { var samurais = db.Samurais.Where(s => EF.Functions.Like(s.Name, "S%")).ToList(); foreach (var item in samurais) { item.Name = "sen" + item.Name; } db.SaveChanges(); } }
private static void InsertSamurai1() { var samurai = new Samurai { Name = "Kire" }; using (var context = new SamuraiDbContext()) { context.Samurais.Add(samurai); context.SaveChanges(); } }
//update Battles public static void UpdateQueryBattle_Disconnected() { var obj = _context.Battles.FirstOrDefault(); obj.EndDate = new DateTime(2000, 01, 01); using (var NewContext = new SamuraiDbContext()) { NewContext.Battles.Update(obj); NewContext.SaveChanges(); } }
private static void RetornaTodosSamurais() { using (var context = new SamuraiDbContext()) { var samurais = context.Samurais.ToList(); var query = context.Samurais; foreach (var s in samurais) { Console.WriteLine(s.Name); } } }
private static void SimpleSamuraiQuery() { using (var context = new SamuraiDbContext()) { var samurais = context.Samurais.ToList(); //var query = context.Samurais; //var samuraisAgain = query.ToList(); foreach (var samurai in context.Samurais) { Console.WriteLine(samurai.Name); } } }
private static void UpdateDisconectedEntity() { var samurai = _context.Samurais.Include(s => s.Quotes).Single(s => s.Id == 14); var quote = samurai.Quotes.First(); quote.Text += " New One"; using (var dbNew = new SamuraiDbContext()) { dbNew.Entry(quote).State = EntityState.Deleted; dbNew.SaveChanges(); } }
private static void AddChildToExistingObjectWhileNotTracked(int samuraiId) { var quote = new Quote { Text = "Now that I saved you, will you feed me dinner?", SamuraiId = samuraiId }; using (var newContext = new SamuraiDbContext()) { newContext.Quotes.Add(quote); newContext.SaveChanges(); } }
private static void MoreQueries() { using (var context = new SamuraiDbContext()) { var samurais_NonParameterizedQuery = context.Samurais.Where(s => s.Name == "Sampson").ToList(); var name = "Sampson"; var samurais_ParameterizedQuery = context.Samurais.Where(s => s.Name == name).ToList(); var samurai_Object = context.Samurais.FirstOrDefault(s => s.Name == name); var samurais_ObjectFindByKeyValue = context.Samurais.Find(2); var samuraisJ = context.Samurais.Where(s => EF.Functions.Like(s.Name, "K%")).ToList(); var search = "K%"; var samuraisJParameter = context.Samurais.Where(s => EF.Functions.Like(s.Name, search)).ToList(); } }
public static void AddQuoteDisc() { var quote = new Quote { Text = "Quote 4", SamuraiId = 11 }; using (var db = new SamuraiDbContext()) { db.Quotes.Add(quote); db.SaveChanges(); } }
private static void InsertMultipleSamurais() { var samurai = new Samurai { Name = "Kire" }; var samuraiSammy = new Samurai { Name = "Sampson" }; using (var context = new SamuraiDbContext()) { context.Samurais.AddRange(samurai, samuraiSammy); context.SaveChanges(); } }
private static void UpdateSamurai() { using (var db = new SamuraiDbContext()) { db.Samurais.Add(new Samurai { Name = "Temp" }); var s1 = db.Samurais.SingleOrDefault(s => s.Id == 1); if (s1 != null) { s1.Name = "sen" + s1.Name; } db.SaveChanges(); } }
private static void DeleteSamurai() { Samurai s; using (var db = new SamuraiDbContext()) { s = db.Samurais.First(s => EF.Functions.Like(s.Name, "S%")); } using (var dbNew = new SamuraiDbContext()) { dbNew.Samurais.Remove(s); dbNew.SaveChanges(); } }
private static void UpdateDisconectedObj() { Samurai s; using (var db = new SamuraiDbContext()) { s = db.Samurais.Single(s => s.Id == 4); } s.Name = "Dis" + s.Name; using (var dbNew = new SamuraiDbContext()) { dbNew.Samurais.Update(s); dbNew.SaveChanges(); } }
public static void GetSamuraiWithQuotes() { using (var db = new SamuraiDbContext()) { var samurais = db.Samurais.Include(s => s.Quotes).Where(s => EF.Functions.Like(s.Name, "K%")).ToList(); foreach (var samurai in samurais) { Console.WriteLine($"{samurai.Id} {samurai.Name}"); Console.WriteLine("--------------------------------------"); foreach (var quote in samurai.Quotes) { Console.WriteLine($"{quote.Id} {quote.Text} {quote.SamuraiId}"); } } } }
public static void QuerySamurai() { using (var db = new SamuraiDbContext()) { var temp = "Kire"; var samurais = db.Samurais.Where(s => s.Name == temp).ToList(); foreach (var samurai in samurais) { Console.WriteLine($"{samurai.Id} {samurai.Name}"); } //var samurai = samurais.Where(s => s.Id == 1).Single(); var sam = samurais.SingleOrDefault(s => s.Id == 1); Console.WriteLine($"Samurai: {sam.Id} {sam.Name}"); } }
private static void InsertSamurais() { var samurai = new Samurai { Name = "Everton mota" }; var samuraiM = new Samurai { Name = "Mr Samurai" }; using (var context = new SamuraiDbContext()) { context.Samurais.AddRange(samurai, samuraiM); context.SaveChanges(); } }
public static void InsertSamurai() { var samurai = new Samurai(); samurai.Name = "Kire"; var sam2 = new Samurai(); sam2.Name = "Petar"; using (var db = new SamuraiDbContext()) { db.Samurais.Add(samurai); db.Add(sam2); db.SaveChanges(); } }
private static void InsertMultipleSamurais() { var samurai = new Samurai { Name = "MR. M" }; var battle = new Battle { Name = "Senhor dos Aneis", StartDate = new DateTime(1800, 05, 25), EndDate = new DateTime(1900, 05, 01) }; using (var context = new SamuraiDbContext()) { context.AddRange(samurai, battle); context.SaveChanges(); } }
//Додадете на битката со id = 1 самураите со Id = 2 и Id = 10(со object tracking и discontinued scenario) private static void UpdateBattle1SamuraiObjTrc() { using (var db = new SamuraiDbContext()) { Battle b = db.Battles.Single(b => b.Id == 1); b.SamuraiBattles = new List <SamuraiBattle> { new SamuraiBattle { SamuraiId = 14 }, new SamuraiBattle { SamuraiId = 7 } }; db.SaveChanges(); } }
//Додаете нова битка во која учествувал самурајот со Id = 11 i еден нов самурај кој има Quote и SecretIdentity а и учествувал во битката со id = 1 private static void InsertBattleSamurai11() { var battle = new Battle { Name = "battle1" }; battle.SamuraiBattles = new List <SamuraiBattle> { new SamuraiBattle { SamuraiId = 11 } }; var samurai = new Samurai { Name = "Tayfun", Quotes = new List <Quote> { new Quote { Text = "hello" } }, SecretIdentity = new SecretIdentity { RealName = "samTayfun" } }; samurai.Battles = new List <SamuraiBattle> { new SamuraiBattle { BattleId = 1 } }; battle.SamuraiBattles.Add(new SamuraiBattle { Samurai = samurai }); using (var db = new SamuraiDbContext()) { db.Battles.Add(battle); db.SaveChanges(); } }