Пример #1
0
        public ActionResult Create([Bind(Include = "QuestionID,UserID,question,answer")] Questions questions)
        {
            if (ModelState.IsValid)
            {
                db.Question.Add(questions);
                db.SaveChanges();
                return(RedirectToAction("Index", new { QuestionID = questions.QuestionID }));
            }

            return(View(questions));
        }
Пример #2
0
        public ActionResult Create([Bind(Include = "UserID,UserEmail,Password,firstName,lastName")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                StarWarsWebsite.Controllers.GlobalVariables.currentUser     = user.UserEmail;
                StarWarsWebsite.Controllers.GlobalVariables.currentUserName = user.firstName;
                StarWarsWebsite.Controllers.GlobalVariables.currentUserID   = user.UserID;
                return(RedirectToAction("Index", "Home"));
            }

            return(View(user));
        }
Пример #3
0
        void SeedData(StarWarsContext context)
        {
            var character = new[]
            {
                new Character {
                    Id = 1, Name = "Gandalf"
                },
                new Character {
                    Id = 2, Name = "Aragorn"
                },
                new Character {
                    Id = 3, Name = "Legolas"
                },
                new Character {
                    Id = 4, Name = "Biblo Baggins"
                },
                new Character {
                    Id = 5, Name = "Eragorn"
                },
                new Character {
                    Id = 6, Name = "Bron"
                },
            };

            context.Characters.AddRange(character);
            context.SaveChanges();
        }
Пример #4
0
 /// <summary>
 /// Adding new Starship items
 /// </summary>
 public static void Insert(List <StarShip> entities)
 {
     using (var context = new StarWarsContext())
     {
         context.StarShip.AddRange(entities);
         context.SaveChanges();
     }
 }
 public void CleanUp()
 {
     if (context != null)
     {
         RemoveCharacterByName(context, testCharacterName);
         RemoveCharacterByName(context, testCharacterNameAfterChange);
         context.SaveChanges();
         context.Dispose();
     }
 }
Пример #6
0
        public void CanPersistEntity()
        {
            // Arrange
            var episodes = new List <EpisodeEntity>();
            var friends  = new List <FriendEntity>();

            episodes.Add(new EpisodeEntity(Guid.NewGuid(), "New Hope"));
            episodes.Add(new EpisodeEntity(Guid.NewGuid(), "Empire"));
            friends.Add(new FriendEntity(Guid.NewGuid(), "R2D2"));
            friends.Add(new FriendEntity(Guid.NewGuid(), "HanSolo"));
            var characterEntity = new StarWarsCharacterEntity(Guid.NewGuid(), "Luke", episodes, friends, "Alderaan");

            // Act

            _context.StarWarsCharacters.Add(characterEntity);
            var result = _context.SaveChanges();

            // Assert
            Assert.NotEqual(0, result);
        }
Пример #7
0
 public static void EnsureSeedData(this StarWarsContext db)
 {
     if (!db.Droids.Any())
     {
         var droid = new Droid
         {
             Name = "R2-D2"
         };
         db.Droids.Add(droid);
         db.SaveChanges();
     }
 }
Пример #8
0
        private static void InsertData(StarWarsContext db)
        {
            db.Humans.Add(new Human
            {
                HumanId    = 1,
                Name       = "Luke",
                HomePlanet = "Tatooine"
            });

            db.Humans.Add(new Human
            {
                HumanId    = 2,
                Name       = "Vader",
                HomePlanet = "Tatooine"
            });

            db.Droids.Add(new Droid
            {
                DroidId         = 1,
                Name            = "R2-D2",
                PrimaryFunction = "Astromech"
            });

            db.Droids.Add(new Droid
            {
                DroidId         = 2,
                Name            = "C-3PO",
                PrimaryFunction = "Protocol"
            });

            db.HumanFriends.Add(new HumanFreind
            {
                HumanId = 1,
                DroidId = 1
            });

            db.HumanFriends.Add(new HumanFreind
            {
                HumanId = 1,
                DroidId = 2
            });

            db.HumanFriends.Add(new HumanFreind
            {
                HumanId = 2,
                DroidId = 1
            });

            var count = db.SaveChanges();

            Console.WriteLine("{0} records saved to database", count);
        }
Пример #9
0
        public async Task <T> Delete(int id)
        {
            T item = await dbContext.Set <T>().FindAsync(id);

            if (item != null)
            {
                dbContext.Set <T>().Remove(item);
            }

            dbContext.SaveChanges();

            return(item);
        }
Пример #10
0
 public static void EnsureSeedData(this StarWarsContext db)
 {
     db._logger.LogInformation("Seeding database");
     if (!db.Droids.Any())
     {
         db._logger.LogInformation("Seeding droids");
         var droid = new Droid
         {
             Name = "R2-D2"
         };
         db.Droids.Add(droid);
         db.SaveChanges();
     }
 }
Пример #11
0
        public IActionResult Post([FromBody] Person person)
        {
            // Calling .Add will mark all entities in the graph as Added
            //_context.People.Add(person);

            // Calling .Attach will mark all entities in the graph as unmodified
            //_context.People.Attach(person);

            // Setting the state of a single entity will not attach related entities
            //_context.Entry(person).State = EntityState.Added;

            // Traverse the graph and apply custom rules to decide when
            // an entity is added, and when it is modified

            /*
             * _context.ChangeTracker.TrackGraph(person,
             *  node =>
             *  {
             *      if (node.Entry.Entity is Starship)
             *      {
             *          // Existing Starship
             *          if ((node.Entry.Entity as Starship).Id > 0)
             *          {
             *              node.Entry.State = EntityState.Modified;
             *          }
             *          else // New Starship
             *          {
             *              node.Entry.State = EntityState.Added;
             *          }
             *      }
             *      else
             *      {
             *          // Not a Starship (Person?). Assume new
             *          node.Entry.State = EntityState.Added;
             *      }
             *  });
             */
            try
            {
                _context.Entry(person).Property("LastUpdated").CurrentValue = DateTime.Now;
                _context.SaveChanges();
                return(new CreatedAtRouteResult("GetPerson", new { id = person.Id }, person));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.ToString()));
            }
        }
Пример #12
0
        public DroidRepositoryShould()
        {
            // Given
            var dbLogger = new Mock <ILogger <StarWarsContext> >();
            // https://docs.microsoft.com/en-us/ef/core/miscellaneous/testing/in-memory
            var options = new DbContextOptionsBuilder <StarWarsContext>()
                          .UseInMemoryDatabase(databaseName: "StarWars")
                          .Options;

            using (var context = new StarWarsContext(options, dbLogger.Object))
            {
                context.Droids.Add(new Droid {
                    Id = 1, Name = "R2-D2"
                });
                context.SaveChanges();
            }
            var starWarsContext = new StarWarsContext(options, dbLogger.Object);
            var repoLogger      = new Mock <ILogger <DroidRepository> >();

            _droidRepository = new DroidRepository(starWarsContext, repoLogger.Object);
        }
 /// <summary>
 /// Clearing data
 /// </summary>
 public static void ClearData()
 {
     using (var context = new StarWarsContext())
     {
         context.Database.ExecuteSqlRaw("DELETE FROM	FilmPeople");
         context.Database.ExecuteSqlRaw("DELETE FROM	Filmplanet");
         context.Database.ExecuteSqlRaw("DELETE FROM filmspecies");
         context.Database.ExecuteSqlRaw("DELETE FROM filmstarship");
         context.Database.ExecuteSqlRaw("DELETE FROM filmvehicle");
         context.Database.ExecuteSqlRaw("DELETE FROM peoplespecies");
         context.Database.ExecuteSqlRaw("DELETE FROM peoplestarship");
         context.Database.ExecuteSqlRaw("DELETE FROM peoplevehicle");
         context.Database.ExecuteSqlRaw("DELETE FROM film");
         context.Database.ExecuteSqlRaw("DELETE FROM people");
         context.Database.ExecuteSqlRaw("DELETE FROM planet");
         context.Database.ExecuteSqlRaw("DELETE FROM species");
         context.Database.ExecuteSqlRaw("DELETE FROM starship");
         context.Database.ExecuteSqlRaw("DELETE FROM vehicle");
         context.SaveChanges();
     }
 }
Пример #14
0
        private void SeedStarWars(StarWarsContext context)
        {
            var episodesList = new List <Episode>
            {
                new Episode
                {
                    Name = "NEWHOPE"
                },
                new Episode
                {
                    Name = "EMPIRE"
                },
                new Episode
                {
                    Name = "JEDI"
                }
            };

            context.Episodes.AddRange(episodesList);
            context.SaveChanges();
            var charactersList = new List <Character>
            {
                new Character
                {
                    Name     = "Luke Skywalker",
                    Episodes = episodesList
                },
                new Character
                {
                    Name     = "Han Solo",
                    Episodes = episodesList
                },
                new Character
                {
                    Name     = "Leia Organa",
                    Episodes = episodesList,
                    Planet   = "Alderaan"
                },
                new Character
                {
                    Name     = "Darth Vader",
                    Episodes = episodesList
                },
                new Character
                {
                    Name     = "Wilhuff Tarkin",
                    Episodes = new List <Episode>
                    {
                        episodesList.ElementAt(0)
                    }
                },
                new Character
                {
                    Name     = "C-3PO",
                    Episodes = episodesList
                },
                new Character
                {
                    Name     = "R2-D2",
                    Episodes = episodesList
                }
            };

            AddFriends(charactersList);
            context.Characters.AddRange(charactersList);
            context.SaveChanges();
        }
Пример #15
0
 public Character insert(Character obj)
 {
     entities.Add(obj);
     context.SaveChanges();
     return(obj);
 }
Пример #16
0
 /// <summary>
 /// Save database. Returns false on fail.
 /// </summary>
 /// <returns></returns>
 public bool Save()
 {
     return(context.SaveChanges() >= 0);
 }
        public static void EnsureSeedData(this StarWarsContext db)
        {
            var newhope = new Episode {
                Id = 4, Title = "NEWHOPE"
            };
            var empire = new Episode {
                Id = 5, Title = "EMPIRE"
            };
            var jedi = new Episode {
                Id = 6, Title = "JEDI"
            };
            var episodes = new List <Episode>
            {
                newhope,
                empire,
                jedi
            };

            if (!db.Episodes.Any())
            {
                db.Episodes.AddRange(episodes);
                db.SaveChanges();
            }

            var tatooine = new Planet {
                Id = 1, Name = "Tatooine"
            };
            var alderaan = new Planet {
                Id = 2, Name = "Alderaan"
            };
            var planets = new List <Planet>
            {
                tatooine,
                alderaan
            };

            if (!db.Planets.Any())
            {
                db.Planets.AddRange(planets);
                db.SaveChanges();
            }

            var luke = new Human
            {
                Id   = 1000,
                Name = "Luke Skywalker",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                HomePlanet = tatooine
            };
            var vader = new Human
            {
                Id   = 1001,
                Name = "Darth Vader",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                HomePlanet = tatooine
            };
            var han = new Human
            {
                Id   = 1002,
                Name = "Han Solo",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                HomePlanet = tatooine
            };
            var leia = new Human
            {
                Id   = 1003,
                Name = "Leia Organa",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                HomePlanet = alderaan
            };
            var tarkin = new Human
            {
                Id   = 1004,
                Name = "Wilhuff Tarkin",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    }
                }
            };
            var humans = new List <Human>
            {
                luke,
                vader,
                han,
                leia,
                tarkin
            };

            if (!db.Humans.Any())
            {
                db.Humans.AddRange(humans);
                db.SaveChanges();
            }

            var threepio = new Droid
            {
                Id   = 2000,
                Name = "C-3PO",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                PrimaryFunction = "Protocol"
            };
            var artoo = new Droid
            {
                Id   = 2001,
                Name = "R2-D2",
                CharacterEpisodes = new List <CharacterEpisode>
                {
                    new CharacterEpisode {
                        Episode = newhope
                    },
                    new CharacterEpisode {
                        Episode = empire
                    },
                    new CharacterEpisode {
                        Episode = jedi
                    }
                },
                PrimaryFunction = "Astromech"
            };
            var droids = new List <Droid>
            {
                threepio,
                artoo
            };

            if (!db.Droids.Any())
            {
                db.Droids.AddRange(droids);
                db.SaveChanges();
            }

            // update character's friends
            luke.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = han
                },
                new CharacterFriend {
                    Friend = leia
                },
                new CharacterFriend {
                    Friend = threepio
                },
                new CharacterFriend {
                    Friend = artoo
                }
            };
            vader.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = tarkin
                }
            };
            han.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = luke
                },
                new CharacterFriend {
                    Friend = leia
                },
                new CharacterFriend {
                    Friend = artoo
                }
            };
            leia.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = luke
                },
                new CharacterFriend {
                    Friend = han
                },
                new CharacterFriend {
                    Friend = threepio
                },
                new CharacterFriend {
                    Friend = artoo
                }
            };
            tarkin.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = vader
                }
            };
            threepio.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = luke
                },
                new CharacterFriend {
                    Friend = han
                },
                new CharacterFriend {
                    Friend = leia
                },
                new CharacterFriend {
                    Friend = artoo
                }
            };
            artoo.CharacterFriends = new List <CharacterFriend>
            {
                new CharacterFriend {
                    Friend = luke
                },
                new CharacterFriend {
                    Friend = han
                },
                new CharacterFriend {
                    Friend = leia
                }
            };
            var characters = new List <Character>
            {
                luke,
                vader,
                han,
                leia,
                tarkin,
                threepio,
                artoo
            };

            if (!db.CharacterFriends.Any())
            {
                db.Characters.UpdateRange(characters);
                db.SaveChanges();
            }

            // update episode's heroes
            newhope.Hero = artoo;
            empire.Hero  = luke;
            jedi.Hero    = artoo;
            db.SaveChanges();
        }
Пример #18
0
 public T insert(T obj)
 {
     entities.Add(obj);
     context.SaveChanges();
     return(obj);
 }