Exemplo n.º 1
0
        public void ManyToMany()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <VirtualStoreContext>());
            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                var genre1 = new Genre()
                {
                    Description = "Ciencia Ficción"
                };
                context.Genres.Add(genre1);
                var genre2 = new Genre()
                {
                    Description = "Fantasia"
                };
                context.Genres.Add(genre2);

                context.SaveChanges();

                var book1 = new Book()
                {
                    Title           = "El Silmarillon",
                    Description     = @"El Silmarillon cuenta la historia de ... bla bla bla",
                    Price           = 139,
                    ISBN            = 1452356565,
                    PublicationYear = 2002,
                    Stock           = 5,
                    Author          = "J. R. R. Tolkien",
                    Genre           = new List <Genre>()
                    {
                        genre1, genre2
                    }
                };

                context.Products.Add(book1);

                var book2 = new Book()
                {
                    Title           = "El Nombre del viento",
                    Description     = @"El Nombre del viento cuenta la historia de ... bla bla bla",
                    Price           = 319,
                    ISBN            = 1452121512,
                    PublicationYear = 2009,
                    Stock           = 10,
                    Author          = "Patrick",
                    Genre           = new List <Genre>()
                    {
                        genre1
                    }
                };

                context.Products.Add(book2);

                context.SaveChanges();

                var books = context.Products.OfType <Book>().ToList();

                Assert.IsNotNull(books);
            }
        }
        public virtual void Create(T entity, List <Expression <Func <T, object> > > unchangeProp)
        {
            // se obtiene la lista de propiedades que deben marcarse con el estado Unchanged
            List <string> unchangelist = unchangeProp.Select(x => ((MemberExpression)x.Body).Member.Name).ToList();

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                context.Set <T>().Add(entity);

                if (unchangeProp != null)
                {
                    // se toma la instancia del objeto que esta asignada a la propiedad
                    // y se asigna el estodo Unchanged
                    foreach (string property in unchangelist)
                    {
                        PropertyInfo propertyInfo = typeof(T).GetProperty(property);
                        var          value        = propertyInfo.GetValue(entity, null);

                        context.Entry(value).State = EntityState.Unchanged;
                    }
                }

                context.SaveChanges();
            }
        }
        public void RemoveEmployees(Territory territory, List <Employee> employees)
        {
            //validamos que haya algo que remover
            if (employees == null || employees.Count == 0)
            {
                return;
            }

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                //recuperamos el terrotorio y sus empleados
                //esto es necesario porque el objeto donde se debe remover tiene que estar dentro del contexto de EF
                Territory territorySel = context.Set <Territory>().Include("Employees").FirstOrDefault(x => x.TerritoryId == territory.TerritoryId);

                if (territory.Employees == null || territory.Employees.Count == 0)
                {
                    return;
                }

                employees.ForEach(x =>
                {
                    //localizamos al empleado dentro de la coleccion que se recupero anteriormente
                    Employee employeeRemove = territorySel.Employees.First(e => e.EmployeeId == x.EmployeeId);
                    //se remueve de la coleccion haciendo uso de la instancia
                    territorySel.Employees.Remove(employeeRemove);
                });

                context.SaveChanges();
            }
        }
        public void AddEmployees(Territory territory, List <Employee> employes)
        {
            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                //marcamos el territorio para que no reciba cambios
                context.Entry(territory).State = EntityState.Unchanged;

                if (territory.Employees == null)
                {
                    territory.Employees = new List <Employee>();
                }

                //recorremos cada empleado que se quiera asociar
                employes.ForEach(x =>
                {
                    //el empleado tampoco debe recibir cambios
                    context.Entry(x).State = EntityState.Unchanged;
                    //asociamos a la colecion de empleados del territorio el nuevo item
                    //este si recibira cambios
                    territory.Employees.Add(x);
                });

                context.SaveChanges();
            }
        }
Exemplo n.º 5
0
        public void Initialize()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <VirtualStoreContext>());

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                context.Customers.RemoveRange(context.Customers);

                customer1 = new Customer()
                {
                    FirstName = "Andres",
                    LastName  = "Perez",
                    BirdDate  = DateTime.Now.AddYears(-20),
                    Residence = new Address()
                    {
                        Country = "Peru",
                        City    = "Huancayo",
                        Street  = "Av Real",
                        Number  = 1300
                    },
                    Email = "*****@*****.**"
                };
                context.Customers.Add(customer1);

                customer2 = new Customer()
                {
                    FirstName = "Eduardo",
                    LastName  = "Quispe",
                    BirdDate  = DateTime.Now.AddYears(-24),
                    Residence = new Address()
                    {
                        Country = "Argentina",
                        City    = "Tambo",
                        Street  = "Av mariscal castilla",
                        Number  = 4522
                    },
                    Email = "*****@*****.**"
                };
                context.Customers.Add(customer2);

                customer3 = new Customer()
                {
                    FirstName = "Manuel",
                    LastName  = "Yupanqui",
                    BirdDate  = DateTime.Now.AddYears(-30),
                    Residence = new Address()
                    {
                        Country = "Argentina",
                        City    = "Chilca",
                        Street  = "Av Leoncio Prado",
                        Number  = 2500
                    },
                    Email = "*****@*****.**",
                };
                context.Customers.Add(customer3);

                context.SaveChanges();
            }
        }
 public void Delete(T entity)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         context.Entry(entity).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
 public void Create(T entity)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         context.Set <T>().Add(entity);
         context.SaveChanges();
     }
 }
 public void Delete(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         var entities = context.Set <T>().Where(predicate).ToList();
         entities.ForEach(x => context.Entry(x).State = EntityState.Deleted);
         context.SaveChanges();
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// Elimina la entidad incluyendo la informacion extendida
        /// en caso de tenerla
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteIncludeExtended(Employee entity)
        {
            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                if (entity.EmployeeExt == null)
                {
                    entity.EmployeeExt = new EmployeeExtended()
                    {
                        EmployeeId = entity.EmployeeId
                    }
                }
                ;

                context.Employees.Attach(entity);
                context.Employees.Remove(entity);

                context.SaveChanges();
            }
        }
    }
 public void AddCollaborator(Collaborator collaborator)
 {
     _context.Collaborators.Add(collaborator);
     _context.SaveChanges();
 }
 public void AddClient(Client client)
 {
     _context.Clients.Add(client);
     _context.SaveChanges();;
 }
Exemplo n.º 12
0
 public void AddNewsletter(NewsletterEmail newsletter)
 {
     _context.newsletters.Add(newsletter);
     _context.SaveChanges();
 }
Exemplo n.º 13
0
        public void OneToMany()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <VirtualStoreContext>());

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                //context.Customers.RemoveRange(context.Customers);

                book1 = new Book()
                {
                    Title           = "El Silmarillon",
                    Description     = @"El Silmarillon cuenta la historia de ... bla bla bla",
                    Price           = 139,
                    ISBN            = 1452356565,
                    PublicationYear = 2002,
                    Stock           = 5,
                    Author          = "J. R. R. Tolkien"
                };
                context.Products.Add(book1);

                movie1 = new Movie()
                {
                    Title         = "El Hombre Araña 3",
                    Description   = @"El Hombre Araña 3 cuenta la historia de ... bla bla bla",
                    Price         = 200,
                    Duration      = new TimeSpan(2, 0, 0),
                    LanguageSound = "Español/Ingles",
                    Stock         = 3
                };
                context.Products.Add(movie1);

                context.SaveChanges();

                customer1 = new Customer()
                {
                    FirstName = "Andres",
                    LastName  = "Perez",
                    BirdDate  = DateTime.Now.AddYears(-20),
                    Residence = new Address()
                    {
                        Country = "Peru",
                        City    = "Huancayo",
                        Street  = "Av Real",
                        Number  = 1300
                    },
                    Email = "*****@*****.**",

                    ShoppingCarts = new List <ShoppingCart>()
                    {
                        new ShoppingCart()
                        {
                            PurchaseDate = DateTime.Now.AddMonths(-1),
                            TotalAmount  = 480,
                            Discount     = 0,
                            Items        = new List <ShoppingItem>()
                            {
                                new ShoppingItem()
                                {
                                    ProductID = book1.ProductID,
                                    Quantity  = 2
                                },
                                new ShoppingItem()
                                {
                                    ProductID = movie1.ProductID,
                                    Quantity  = 1
                                }
                            }
                        },
                        new ShoppingCart()
                        {
                            PurchaseDate = DateTime.Now,
                            TotalAmount  = 140,
                            Discount     = 20,
                            Items        = new List <ShoppingItem>()
                            {
                                new ShoppingItem()
                                {
                                    ProductID = book1.ProductID,
                                    Quantity  = 1
                                }
                            }
                        }
                    }
                };

                context.Customers.Add(customer1);
                context.SaveChanges();

                var customer = context.Customers.FirstOrDefault(x => x.CustomerId == customer1.CustomerId);
                Assert.IsNotNull(customer);
            }
        }
Exemplo n.º 14
0
        //CARGAR ENTIDADES RELACIONADAS

        public void LlenarDatos()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <VirtualStoreContext>());

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                #region Products
                book1 = new Book()
                {
                    Title           = "El Silmarillon",
                    Description     = @"El Silmarillon cuenta la historia de ... bla bla bla",
                    Price           = 139,
                    ISBN            = 1452356565,
                    PublicationYear = 2002,
                    Stock           = 5,
                    Author          = "J. R. R. Tolkien"
                };
                context.Products.Add(book1);

                book2 = new Book()
                {
                    Title           = "El Nombre del viento",
                    Description     = @"El Nombre del viento cuenta la historia de ... bla bla bla",
                    Price           = 319,
                    ISBN            = 1452121512,
                    PublicationYear = 2009,
                    Stock           = 10,
                    Author          = "Patrick"
                };
                context.Products.Add(book2);

                book3 = new Book()
                {
                    Title           = "Un Mago de Terramar",
                    Description     = @"Un Mago de Terramar cuenta la historia de ... bla bla bla",
                    Price           = 217,
                    ISBN            = 1452342344,
                    PublicationYear = 1968,
                    Stock           = 3,
                    Author          = "Ursula"
                };
                context.Products.Add(book3);

                movie1 = new Movie()
                {
                    Title         = "El Hombre Araña 3",
                    Description   = @"El Hombre Araña 3 cuenta la historia de ... bla bla bla",
                    Price         = 200,
                    Duration      = new TimeSpan(2, 0, 0),
                    LanguageSound = "Español/Ingles",
                    Stock         = 3
                };
                context.Products.Add(movie1);

                movie2 = new Movie()
                {
                    Title         = "Capitan America",
                    Description   = @"Capitan America cuenta la historia de ... bla bla bla",
                    Price         = 100,
                    Duration      = new TimeSpan(2, 30, 0),
                    LanguageSound = "Español/Ingles",
                    Stock         = 5
                };
                context.Products.Add(movie2);
                context.SaveChanges();
                #endregion

                #region customer1
                customer1 = new Customer()
                {
                    FirstName = "Andres",
                    LastName  = "Perez",
                    BirdDate  = DateTime.Now.AddYears(-20),
                    Residence = new Address()
                    {
                        Country = "Peru",
                        City    = "Huancayo",
                        Street  = "Av Real",
                        Number  = 1300
                    },
                    Email = "*****@*****.**",

                    ShoppingCarts = new List <ShoppingCart>()
                    {
                        new ShoppingCart()
                        {
                            PurchaseDate = DateTime.Now,
                            TotalAmount  = 340,
                            Discount     = 20,
                            Items        = new List <ShoppingItem>()
                            {
                                new ShoppingItem()
                                {
                                    ProductID = book1.ProductID,
                                    Quantity  = 1,
                                    Detail    = new ShoppingItemDetail()
                                    {
                                        SalesMan     = "Hernandez, Juan",
                                        Observations = "Se aplica descuento"
                                    }
                                },
                                new ShoppingItem()
                                {
                                    ProductID = book3.ProductID,
                                    Quantity  = 1
                                }
                            }
                        }
                    }
                };

                context.Customers.Add(customer1);
                #endregion

                #region customer2
                //customer2 = new Customer()
                //{
                //    FirstName = "Andres",
                //    LastName = "Perez",
                //    BirdDate = DateTime.Now.AddYears(-20),
                //    Residence = new Address()
                //    {
                //        Country = "Peru",
                //        City = "Huancayo",
                //        Street = "Av Real",
                //        Number = 1300
                //    },
                //    Email = "*****@*****.**",

                //    ShoppingCarts = new List<ShoppingCart>()
                //    {
                //        new ShoppingCart()
                //        {
                //            PurchaseDate = DateTime.Now.AddMonths(-1),
                //            TotalAmount = 480,
                //            Discount = 0,
                //            Items = new List<ShoppingItem>()
                //            {
                //                new ShoppingItem()
                //                {
                //                    ProductID = book1.ProductID,
                //                    Quantity = 2
                //                },
                //                new ShoppingItem()
                //                {
                //                    ProductID = movie1.ProductID,
                //                    Quantity = 1
                //                }
                //            }
                //        },
                //        new ShoppingCart()
                //        {
                //            PurchaseDate = DateTime.Now,
                //            TotalAmount = 140,
                //            Discount = 20,
                //            Items = new List<ShoppingItem>()
                //            {
                //                new ShoppingItem()
                //                {
                //                    ProductID = book1.ProductID,
                //                    Quantity = 1
                //                }
                //            }
                //        }
                //    }
                //};
                //context.Customers.Add(customer2);
                #endregion

                context.SaveChanges();
            }
        }
Exemplo n.º 15
0
        public void OneToOne()
        {
            Database.SetInitializer(new DropCreateDatabaseAlways <VirtualStoreContext>());

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                context.Customers.RemoveRange(context.Customers);

                book1 = new Book()
                {
                    Title           = "El Silmarillon",
                    Description     = @"El Silmarillon cuenta la historia de ... bla bla bla",
                    Price           = 139,
                    ISBN            = 1452356565,
                    PublicationYear = 2002,
                    Stock           = 5,
                    Author          = "J. R. R. Tolkien"
                };

                customer1 = new Customer()
                {
                    FirstName = "Andres",
                    LastName  = "Perez",
                    BirdDate  = DateTime.Now.AddYears(-20),
                    Residence = new Address()
                    {
                        Country = "Peru",
                        City    = "Huancayo",
                        Street  = "Av Real",
                        Number  = 1300
                    },
                    Email = "*****@*****.**",

                    ShoppingCarts = new List <ShoppingCart>()
                    {
                        new ShoppingCart()
                        {
                            PurchaseDate = DateTime.Now,
                            TotalAmount  = 320,
                            Discount     = 20,
                            Items        = new List <ShoppingItem>()
                            {
                                new ShoppingItem()
                                {
                                    Product  = book1,
                                    Quantity = 1,
                                    Detail   = new ShoppingItemDetail()
                                    {
                                        SalesMan     = "Hernandez, Juan",
                                        Observations = "Se aplica descuento"
                                    }
                                }
                            }
                        }
                    }
                };


                context.Customers.Add(customer1);
                context.SaveChanges();

                var customer = context.Customers.FirstOrDefault(x => x.CustomerId == customer1.CustomerId);
                Assert.IsNotNull(customer);
            }
        }