Esempio n. 1
0
        public void Solution10B()
        {
            const string sql = @"update Devices 
                                    set Firmware = '9.9.9.9' 
                                    where Model = 'SL2600'";

            const string deleteSql = @"delete from Contacts where Id = 2011";

            int contactId = 2011;

            using (var context = new RadioContext())
                using (var context2 = new RadioContext(context.Database.Connection))
                    using (var transaction = context.Database.BeginTransaction())
                    {
                        // context 2
                        context2.Database.UseTransaction(transaction.UnderlyingTransaction);
                        Contact contact = context2.Contacts.Find(contactId);
                        contact.Country = "Poland";
                        context2.SaveChanges();

                        // context 1
                        context.Database.ExecuteSqlCommand(deleteSql);
                        context.Contacts.Remove(contact);

                        context.SaveChanges();

                        transaction.Commit();
                    }
        }
        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}");
            }
        }
Esempio n. 3
0
        private StepConfiguration LoadContext(RadioContext context)
        {
            Step step = new Step(context);

            handler.Steps.Add(step);
            return(new StepConfiguration(step));
        }
Esempio n. 4
0
        public void Example13()
        {
            using (var context = new RadioContext())
            {
                context.Database.Log += msg
                                        => WriteOutput(msg, ConsoleColor.Green);


                // note: using System.Data.Entity;
                var calls = context.Calls
                            .Include(p => p.Source)
                            .Include(p => p.Sender)
                            .AsNoTracking()
                            .Select(c => new
                {
                    c.Id,
                    c.Status,
                    c.Source.Model,
                    c.Source.Firmware,
                    c.Sender.Country,
                    c.Sender.CompanyName
                })
                            .ToList();
            }
        }
Esempio n. 5
0
        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());
            }
        }
Esempio n. 6
0
        public void Solution11B()
        {
            int deviceId = 1001;

            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 device1 = context1.Devices.Find(deviceId);
                    device1.Model = $"DP {random.Next(1, 100)}";

                    try
                    {
                        // user 2
                        var device2 = context2.Devices.Find(deviceId);
                        device2.Firmware = $"1.0.0.{random.Next(1, 10)}";
                        context2.SaveChanges();

                        // user 1
                        context1.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException e)
                    {
                        WriteOutput("Another user was changed data", ConsoleColor.Red);
                    }
                }
        }
        public void Solution12()
        {
            UserSearchCriteria criteria = new UserSearchCriteria
            {
                Country = "Poland"
            };

            using (var context = new RadioContext())
            {
                context.Database.Log += msg => WriteOutput(msg, ConsoleColor.Green);

                IQueryable <User> contacts = context.Contacts.OfType <User>();

                if (!string.IsNullOrEmpty(criteria.FirstName))
                {
                    contacts = contacts.Where(c => c.FirstName == criteria.FirstName);
                }

                if (!string.IsNullOrEmpty(criteria.LastName))
                {
                    contacts = contacts.Where(c => c.LastName == criteria.LastName);
                }

                if (!string.IsNullOrEmpty(criteria.Country))
                {
                    contacts = contacts.Where(c => c.Country == criteria.Country);
                }

                List <User> filteredUsers = contacts.Take(100).ToList();
            }
        }
Esempio n. 8
0
        public void Example2()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                context.Configuration.LazyLoadingEnabled   = true;
                context.Configuration.ProxyCreationEnabled = true;

                IQueryable <Device> devices = context.Devices
                                              .Where(d => d.Model == model);

                List <Device> myDevices = devices.ToList();

                foreach (var device in myDevices)
                {
                    // Następuje niepożądane zjawisko - dla każdej encji wykonywane jest zapytanie do bazy danych
                    int qty = device.Calls.Count;

                    // WriteOutput($"{device.Name} - {device.Calls.Count}");
                }

                context.Configuration.LazyLoadingEnabled   = false;
                context.Configuration.ProxyCreationEnabled = false;
            }
        }
        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;
                }
            }
        }
Esempio n. 10
0
        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());
            }
        }
Esempio n. 11
0
        public void Solution9C()
        {
            string model = "DP2000e";

            var config = new MapperConfiguration(
                cfg => cfg.CreateMap <Device, DeviceDTO>()
                .ForMember(dest => dest.FullName,
                           opt => opt.MapFrom(src => src.Model + " " + src.Firmware))
                .ForMember(dest => dest.CallCount,
                           opt => opt.MapFrom(src => src.Calls.Count))
                );

            var mapper = config.CreateMapper();


            using (var context = new RadioContext())
            {
                IQueryable <DeviceDTO> deviceDTOs = context.Devices
                                                    .Where(d => d.Model == model)
                                                    .ProjectTo <DeviceDTO>(config)
                                                    .OrderBy(d => d.CallCount);

                List <DeviceDTO> devices = deviceDTOs.ToList();
            }
        }
Esempio n. 12
0
        // Usuwanie encji z użyciem metody rozszerzacącej na DbContext
        public void Solution6D()
        {
            int userId = 1000;

            using (var context = new RadioContext())
            {
                context.Remove <User>(userId);
                context.SaveChanges();
            }
        }
        public void Example4A()
        {
            using (var context = new RadioContext())
            {
                Device device = context.Devices.First();

                // Wyświetlenie stacji encji
                WriteOutput($"{context.Entry(device).State}");
            }
        }
Esempio n. 14
0
        public void Example1()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                List <Device> devices   = context.Devices.ToList();                      // Pobieramy wszystkie urządzenia
                List <Device> myDevices = devices.Where(d => d.Model == model).ToList(); // Filtrujemy dopiero po stronie klienta
            }
        }
Esempio n. 15
0
        // Usuwanie encji z użyciem metody rozszerzacącej na DbSet
        public void Solution6E()
        {
            int deviceId = 1000;

            using (var context = new RadioContext())
            {
                context.Devices.Remove(deviceId);

                context.SaveChanges();
            }
        }
Esempio n. 16
0
        public void Solution2B()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                List <Device> popularDevices = context.Devices
                                               .Where(d => d.Model == model)
                                               .Where(d => d.Calls.Count > 100)
                                               .ToList();
            }
        }
        public void Solution4A()
        {
            using (var context = new RadioContext())
            {
                Device device = context
                                .Devices
                                .AsNoTracking() // wyłączenie śledzenie zmian
                                .First();

                WriteOutput($"{context.Entry(device).State}");
            }
        }
Esempio n. 18
0
        // Query Syntax
        public void Solution1C()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                List <Device> myDevices = (from d in context.Devices
                                           where d.Model == model
                                           select d)        // utworzenie wyrażenia
                                          .ToList();        // materializacja
            }
        }
Esempio n. 19
0
        // Method Syntax
        public void Solution1B()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                IQueryable <Device> devices = context
                                              .Devices
                                              .Where(d => d.Model == model); // utworzenie wyrażenia

                List <Device> myDevices = devices.ToList();                  // materializacja
            }
        }
        public void Example8()
        {
            int userId = 2004;

            using (var context = new RadioContext())
            {
                User user = context.Contacts.OfType <User>().Single(u => u.Id == userId);

                context.Contacts.Remove(user);

                context.SaveChanges();
            }
        }
Esempio n. 21
0
        // 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();
            }
        }
Esempio n. 22
0
        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());
            }
        }
        public void Example3()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                List <Device> devices = context.Devices
                                        .Where(d => d.Model == model)
                                        .ToList();

                foreach (var device in devices)
                {
                    WriteOutput($"{device.Name} {device.Firmware}");
                }
            }
        }
Esempio n. 24
0
        public void Solution1A()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                // Podpięcie się pod zdarzenie materializacji
                // DbContext tego nie upublicznia dlatego wymagane jest pobranie ObjectContext)
                context.GetObjectContext().ObjectMaterialized += (s, e) => WriteOutput($"Object materialized {e.Entity}", ConsoleColor.DarkMagenta);

                List <Device> myDevices = context
                                          .Devices
                                          .Where(d => d.Model == model) // utworzenie wyrażenia
                                          .ToList();                    // materializacja
            }
        }
Esempio n. 25
0
        public void Example9()
        {
            string country = "Poland";

            using (var context = new RadioContext())
            {
                List <UserDTO> users = context.Contacts
                                       .OfType <User>()
                                       .Where(u => u.Country == country)
                                       .Select(u => new UserDTO
                {
                    FirstName = u.FirstName,
                    Surname   = u.LastName
                })
                                       .ToList();
            }
        }
        public void Solution3()
        {
            string model = "SL2600";

            using (var context = new RadioContext())
            {
                var devices = context.Devices
                              .Where(d => d.Model == model)
                              .Select(d => new { d.Name, d.Firmware })
                              .ToList();

                foreach (var device in devices)
                {
                    WriteOutput($"{device.Name} {device.Firmware}");
                }
            }
        }
Esempio n. 27
0
        public void Example14()
        {
            var model = new Model {
                Page = 2, ResultsPerPage = 10
            };

            using (var context = new RadioContext())
            {
                context.Database.Log += msg => WriteOutput(msg, ConsoleColor.Red);

                List <Device> devices = context.Devices
                                        .OrderBy(d => d.Model)
                                        .Skip(model.Page * model.ResultsPerPage)
                                        .Take(model.ResultsPerPage)
                                        .ToList();
            }
        }
Esempio n. 28
0
        public void Example6C()
        {
            int userId = 2008;

            using (var context = new RadioContext())
            {
                User user = context.Contacts.OfType <User>().Single(u => u.Id == userId);

                // User user = context.Contacts.Include(c=>c.Calls).OfType<User>().Single(u => u.Id == userId);

                context.Contacts.Remove(user);

                var entities = context.ChangeTracker.Entries();

                context.SaveChanges();
            }
        }
Esempio n. 29
0
        public void Example11()
        {
            using (var context1 = new RadioContext())
                using (var context2 = new RadioContext())
                {
                    // user 1
                    var contact1 = context1.Contacts.Find(contactId);
                    contact1.CompanyName = "Altkom 2";

                    // user 2
                    var contact2 = context1.Contacts.Find(contactId);
                    contact2.CompanyName = "Motorola 2";
                    context2.SaveChanges();

                    // user 1
                    context1.SaveChanges();
                }
        }
Esempio n. 30
0
        // PM> Install-Package AutoMapper
        public void Solution9A()
        {
            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 <User> users = context.Contacts
                                          .OfType <User>()
                                          .Where(u => u.Country == country);

                List <UserDTO> userDTOs = mapper.Map <List <UserDTO> >(users);
            }
        }