Example #1
0
 public Pet Update(Pet petUpdate)
 {
     _ctx.Attach(petUpdate).State = EntityState.Modified;
     _ctx.Entry(petUpdate).Reference(o => o.PreviousOwner).IsModified = true;
     _ctx.SaveChanges();
     return(petUpdate);
 }
Example #2
0
        public static void SeedDB(PetShopAppContext context)
        {
            // Create the database, if it does not already exists. If the database
            // already exists, no action is taken (and no effort is made to ensure it
            // is compatible with the model for this context).
            context.Database.EnsureCreated();

            // Look for any TodoItems
            //if (context.TodoItems.Any())
            //{
            //    return;   // DB has been seeded
            //}

            List <TodoItem> items = new List <TodoItem>
            {
                new TodoItem {
                    IsComplete = true, Name = "Make homework"
                },
                new TodoItem {
                    IsComplete = false, Name = "Sleep"
                }
            };

            // Create two users with hashed and salted passwords
            string password = "******";

            byte[] passwordHashJoe, passwordSaltJoe, passwordHashAnn, passwordSaltAnn;
            CreatePasswordHash(password, out passwordHashJoe, out passwordSaltJoe);
            CreatePasswordHash(password, out passwordHashAnn, out passwordSaltAnn);

            List <User> users = new List <User>
            {
                new User {
                    Username     = "******",
                    PasswordHash = passwordHashJoe,
                    PasswordSalt = passwordSaltJoe,
                    IsAdmin      = false
                },
                new User {
                    Username     = "******",
                    PasswordHash = passwordHashAnn,
                    PasswordSalt = passwordSaltAnn,
                    IsAdmin      = true
                }
            };

            context.TodoItems.AddRange(items);
            context.Users.AddRange(users);
            context.SaveChanges();
        }