public void Solution4B() { var myDevice = new Device { Id = 99999, Name = "My Radio" }; using (var context = new RadioContext()) { try { context.Configuration.AutoDetectChangesEnabled = false; context.Devices.Add(myDevice); var entities = context.ChangeTracker.Entries(); Device device = context.Devices.AsNoTracking().First(); entities = context.ChangeTracker.Entries(); device.Firmware = "10.0.0.0"; WriteOutput($"{context.Entry(myDevice).State}"); WriteOutput($"{context.Entry(device).State}"); device.Firmware = "10.0.0.0"; WriteOutput($"{context.Entry(device).State}"); } finally { context.Configuration.AutoDetectChangesEnabled = true; } } }
public void Example6() { int userId = 2009; string firstName = "Marcin"; string lastName = "Sulecki"; using (var context = new RadioContext()) { User user = context.Contacts .OfType <User>() .Single(c => c.Id == userId); WriteOutput(context.Entry(user).State.ToString()); user.FirstName = firstName; user.LastName = lastName; WriteOutput(context.Entry(user).State.ToString()); context.SaveChanges(); WriteOutput(context.Entry(user).State.ToString()); user.Country = "Poland"; WriteOutput(context.Entry(user).State.ToString()); context.SaveChanges(); WriteOutput(context.Entry(user).State.ToString()); } }
public void Example4B() { var myDevice = new Device { Id = 99999, Name = "My Radio" }; using (var context = new RadioContext()) { context.Configuration.AutoDetectChangesEnabled = true; // domyślnie włączone context.Devices.Add(myDevice); var entities = context.ChangeTracker.Entries(); Device device = context.Devices.AsNoTracking().First(); entities = context.ChangeTracker.Entries(); device.Firmware = "10.0.0.0"; WriteOutput($"{context.Entry(myDevice).State}"); WriteOutput($"{context.Entry(device).State}"); device.Firmware = "10.0.0.0"; WriteOutput($"{context.Entry(device).State}"); } }
public void Solution9B() { string country = "Poland"; var config = new MapperConfiguration( cfg => cfg.CreateMap <User, UserDTO>() .ForMember(dest => dest.Surname, opt => opt.MapFrom(src => src.LastName))); var mapper = config.CreateMapper(); using (var context = new RadioContext()) { IQueryable <UserDTO> users = context.Contacts .OfType <User>() .Where(u => u.Country == country) .ProjectTo <UserDTO>(config) .OrderBy(u => u.Surname); List <UserDTO> userDTOs = users.ToList(); var userDTO = userDTOs.First(); WriteOutput(context.Entry(userDTO).State.ToString()); } }
public void Example4A() { using (var context = new RadioContext()) { Device device = context.Devices.First(); // Wyświetlenie stacji encji WriteOutput($"{context.Entry(device).State}"); } }
public void Solution4A() { using (var context = new RadioContext()) { Device device = context .Devices .AsNoTracking() // wyłączenie śledzenie zmian .First(); WriteOutput($"{context.Entry(device).State}"); } }
public void Solution6B() { int userId = 747474; string firstName = "Bartek"; string lastName = "Sulecki"; string country = "Country"; string companyName = "Altkom"; // symulacja deserializacji User user = new User { Id = userId, FirstName = firstName, LastName = lastName, Country = country, CompanyName = companyName }; using (var context = new RadioContext()) { WriteOutput(context.Entry(user).State.ToString()); context.Contacts.Attach(user); WriteOutput(context.Entry(user).State.ToString()); context.Entry(user).State = System.Data.Entity.EntityState.Modified; WriteOutput(context.Entry(user).State.ToString()); var entities = context.ChangeTracker.Entries() .Select(x => new { x.Entity, x.State }); context.SaveChanges(); WriteOutput(context.Entry(user).State.ToString()); } }
public void Solution11A() { using (var context1 = new RadioContext()) using (var context2 = new RadioContext()) { context1.Database.Log += msg => WriteOutput(msg, ConsoleColor.Green); context2.Database.Log += msg => WriteOutput(msg, ConsoleColor.Blue); Random random = new Random(); // user 1 var contact1 = context1.Contacts.Find(contactId); contact1.CompanyName = $"Altkom {random.Next(1, 100)}"; try { // user 2 var contact2 = context2.Contacts.Find(contactId); contact2.CompanyName = $"Motorola {random.Next(1, 100)}"; context2.SaveChanges(); // user 1 context1.SaveChanges(); } catch (DbUpdateConcurrencyException e) { WriteOutput("Another user was changed data", ConsoleColor.Red); // var original = context1.Entry(contact1).OriginalValues.ToObject(); var my = context1.Entry(contact1).CurrentValues.ToObject(); // Refresh object from database context1.Entry(contact1).Reload(); var current = context1.Entry(contact1).OriginalValues.ToObject(); } } }
// Usuwanie encji za pomocą bezpośredniej zmiany stanu public void Solution6C() { int userId = 1000; User user = new User { Id = userId }; using (var context = new RadioContext()) { context.Contacts.Attach(user); context.Entry(user).State = EntityState.Deleted; context.SaveChanges(); } }
public void Solution6A() { int userId = 2009; string firstName = "Bartek"; string lastName = "Sulecki"; using (var context = new RadioContext()) { context.Configuration.AutoDetectChangesEnabled = false; User user = context.Contacts .AsNoTracking() .OfType <User>() .Single(c => c.Id == userId); // TODO: Long process WriteOutput(context.Entry(user).State.ToString()); context.Contacts.Attach(user); user.FirstName = firstName; user.LastName = lastName; WriteOutput(context.Entry(user).State.ToString()); WriteOutput(context.Entry(user).State.ToString()); context.ChangeTracker.DetectChanges(); WriteOutput(context.Entry(user).State.ToString()); context.SaveChanges(); WriteOutput(context.Entry(user).State.ToString()); user.Country = "Poland"; WriteOutput(context.Entry(user).State.ToString()); context.SaveChanges(); WriteOutput(context.Entry(user).State.ToString()); } }