public IActionResult CreatePerson([FromBody] PersonModel person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Person personToAdd = new Person { FirstName = person.FirstName, LastName = person.LastName }; if (person.Identifiers != null) { personToAdd.Identifiers = new List <Identifier>(); person.Identifiers.ForEach(x => { personToAdd.Identifiers.Add(new Identifier { Type = x.Type, Value = x.Value }); }); } _db.Persons.Add(personToAdd); _db.SaveChanges(); return(Ok(person)); }
public IHttpActionResult PutPerson(int id, Person person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != person.Id) { return(BadRequest()); } db.Entry(person).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void Manually_Remove_Reference() { using (var personsContext = new PersonsContext(DbContextOptions)) { var him = new Person { Id = 1, Name = "Him", SpouseId = 2 }; var her = new Person { Id = 2, Name = "Her", SpouseId = 1 }; personsContext.Persons.Add(him); personsContext.Persons.Add(her); personsContext.SaveChanges(); } using (var personsContext = new PersonsContext(DbContextOptions)) { var him = personsContext.Persons.Find(1); var her = personsContext.Persons.Find(2); him.SpouseId = null; personsContext.SaveChanges(); her.SpouseId = null; personsContext.SaveChanges(); personsContext.Persons.Remove(him); personsContext.SaveChanges(); } using (var personsContext = new PersonsContext(DbContextOptions)) { Assert.Null(personsContext.Find <Person>(1)); } }
public static void DeletePerson(int personId) { var person = context.Persons.First(p => p.Id == personId); context.PersonsRoles.RemoveRange(context.PersonsRoles.Where(p => p.PersonId == personId)); context.Persons.Remove(person); context.SaveChanges(); }
public ActionResult Create(Person person) { if (ModelState.IsValid) { db.People.Add(person); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(person)); }
public IHttpActionResult Delete(int id) { Person person = db.Persons.Find(id); if (person != null) { db.Persons.Remove(person); db.SaveChanges(); return(Ok()); } return(NotFound()); }
public IHttpActionResult Delete(int id) { Sick sick = db.Sicks.Find(id); if (sick != null) { db.Sicks.Remove(sick); db.SaveChanges(); return(Ok()); } return(NotFound()); }
public IHttpActionResult Delete(int id) { Medicine medicine = db.Medicines.Find(id); if (medicine != null) { db.Medicines.Remove(medicine); db.SaveChanges(); return(Ok()); } return(NotFound()); }
public bool AddPerson([FromBody] Person Person) { _context.Persons.Add(Person); try { _context.SaveChanges(); } catch { return(false); } return(true); }
public void Run() { using (var db = new PersonsContext(Constants.ConnectionString)) { var person = new Person { Firstname = "Travis", LastName = "Howe" }; db.Persons.Add(person); db.SaveChanges(); } var options = new DbContextOptionsBuilder <PersonsContext>() .EnableDetailedErrors() .EnableSensitiveDataLogging() .LogTo(Console.WriteLine) .UseNpgsql(Constants.ConnectionString) .Options; using (var db = new PersonsContext(options)) { var customer = new Person { Firstname = "Edward", LastName = "Chester" }; db.Persons.Add(customer); db.SaveChanges(); } }
static void TryPersonsContext() { using (var context = new PersonsContext()) { var person1 = new Person { FirstName = "Alexandra", MiddleName = "Ioana", LastName = "Ghidersa", TelephoneNumber = "0789369548" }; var person2 = new Person { FirstName = "Ana", MiddleName = "Maria", LastName = "Popescu", TelephoneNumber = "0789309549" }; context.People.AddRange(new Person[] { person1, person2 }); context.SaveChanges(); foreach (var person in context.People) { Console.WriteLine(person); } } }
public void EnsureFillingUpDB() { try { var csvFile = new FavouriteColorCSVFileRepository(); var test = csvFile.GetPeople(); PersonsContext pc = new PersonsContext(); //Dumb cleaning pc.Database.ExecuteSqlCommand("delete from [Person];"); pc.Database.ExecuteSqlCommand("delete from [Color];"); pc.Colors.AddRange(UtilColor.Colors); pc.Persons.AddRange(test); pc.SaveChanges(); Assert.IsTrue(true); } catch (Exception) { Assert.IsTrue(false); } }
public void OnDelete_SetNull() { using (var personsContext = new PersonsContext(DbContextOptions)) { var him = new Person { Id = 1, Name = "Him", SpouseId = 2 }; var her = new Person { Id = 2, Name = "Her", SpouseId = 1 }; personsContext.Persons.Add(him); personsContext.Persons.Add(her); personsContext.SaveChanges(); } using (var personsContext = new PersonsContext(DbContextOptions)) { var him = personsContext.Persons.Find(1); personsContext.Persons.Remove(him); personsContext.SaveChanges(); } using (var personsContext = new PersonsContext(DbContextOptions)) { Assert.Null(personsContext.Find <Person>(1)); } }
private void InitContext() { if (_db == null) { var builder = new DbContextOptionsBuilder <PersonsContext>().UseInMemoryDatabase(); var context = new PersonsContext(builder.Options); var persons = Enumerable.Range(1, 10) .Select(i => new Person { FirstName = $"Person{i}", LastName = $"Last{i}", Identifiers = new List <Identifier> { new Identifier { Type = IdentifierTypes.Email, Value = $"Identifier{i}" }, new Identifier { Type = IdentifierTypes.Email, Value = $"Identifier{i+1}" } } }); context.Persons.AddRange(persons); int changed = context.SaveChanges(); _db = context; } }
static void Main(string[] args) { using (context = new PersonsContext()) { if (context.Persons.Count() <= 0) { context.Persons.Add(new Person("test1", "1")); context.Persons.Add(new Person("test2", "2")); context.Persons.Add(new Person("test3", "3")); context.SaveChanges(); } context.Persons.Load(); } IPAddress ip = IPAddress.Parse("127.0.0.1"); const int PORT = 2020; Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint ep = new IPEndPoint(ip, PORT); socket.Bind(ep); socket.Listen(10);//max queue 10 clients while (true) { const int SIZE = 1024; Socket client = socket.Accept(); byte[] buffer = new byte[SIZE]; int countBytes = 0; do { countBytes = client.Receive(buffer); }while (client.Available > 0); string data = Encoding.UTF8.GetString(buffer, 0, countBytes); Person[] tmp; using (context = new PersonsContext()) { tmp = context.Persons.Where(x => x.Name.Contains(data)).ToArray(); } byte[] response = Formatter.Serialize(tmp); client.Send(response); client.Shutdown(SocketShutdown.Both); client.Close(); } } catch (SocketException e) { Console.WriteLine(e.Message); }; }
public Person AddPerson(EntityModels.Person newPerson) { if (newPerson == null) { throw new ArgumentNullException("newPerson"); } _context.Persons.Add(newPerson); _context.SaveChanges(); return(new Person { FirstName = newPerson.FirstName, LastName = newPerson.LastName, PersonId = newPerson.PersonId }); }
// PUT: odata/Persons(5) public IHttpActionResult Put([FromODataUri] int key, Delta <Person> patch) { Validate(patch.GetEntity()); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Person person = db.Persons.Find(key); if (person == null) { return(NotFound()); } patch.Put(person); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(key)) { return(NotFound()); } else { throw; } } return(Updated(person)); }
public static void Initialize(IServiceProvider serviceProvider) { using (var context = new PersonsContext( serviceProvider.GetRequiredService < DbContextOptions <PersonsContext> >())) { // Look for any movies. if (context.persons.Any()) { return; // DB has been seeded } context.persons.AddRange( new Person { age = 20, hair_color = "Brown", eye_color = "Green", }, new Person { age = 23, hair_color = "Pink", eye_color = "Blue", }, new Person { age = 22, hair_color = "Black", eye_color = "Red", }, new Person { age = 50, hair_color = "Red", eye_color = "Brown", } ); context.SaveChanges(); } }
public bool SavePeople(IEnumerable <Person> people) { if (people.Count() == 0) { throw new NoPeopleForUpdateException(); } if (people.Any(f => f.FavColourId == null)) { throw new ColorIsNullException(); } int idcounter = this._personsContext.Persons.OrderByDescending(f => f.Id).FirstOrDefault().Id + 1; foreach (var item in people) { item.Id = idcounter++; _personsContext.Persons.Add(item); } _personsContext.SaveChanges(); return(true); }
static void Main(string[] args) { //Console.WriteLine("Tomt postnummer/by tilføjes Aarhus, 8000, Denmark."); //ByPostnummer aarhus = new ByPostnummer(8000, "Aarhus", "Denmark"); //Console.WriteLine("Indeholder 0 adresser:"); //var checkpoint = Console.ReadKey(); //aarhus.printAdresses(); //Adresse ad1 = new Adresse("Silkeborgvej", 22, aarhus,"Hjem"); //Adresse ad2 = new Adresse("Silkeborgvej", 23, aarhus, "Hjem"); //Adresse ad3 = new Adresse("Silkeborgvej", 24, aarhus, "Hjem"); //Adresse ad4 = new Adresse("Silkeborgvej", 25, aarhus, "Hjem"); //Console.WriteLine("Tilføjelse af 4 adresser til postnummer via aarhus.printAdresses();:"); //aarhus.printAdresses(); //checkpoint = Console.ReadKey(); //Console.WriteLine(); //Console.WriteLine("Tilføjer nyt telefonnummer samt en enkelt person til adresse Silkeborgvej 22:"); //TelefonNummer JensArbejd = new TelefonNummer(12345678,"Arbejd", "Telia"); //Person Jens = new Person(11111900, "Jens", "Poulsen", JensArbejd, "Kollega", ad1,"*****@*****.**", "Peter"); //Jens.print(); //Jens.printPhone(); //checkpoint = Console.ReadKey(); //Console.WriteLine(); //Console.WriteLine("Tilføjer 3 personer til adresse Silkeborgvej 22:"); //Person Ditte = new Person(11111901, "Ditte", "Poulsen", JensArbejd, "Kollega", ad1); //Person Samuel = new Person(11111902, "Samuel", "Poulsen", JensArbejd, "Studerende", ad1); //Person Abdi = new Person(11111903, "Abdi", "Poulsen", JensArbejd, "Kontanthjælp", ad1,"*****@*****.**"); //Console.WriteLine(); //Console.WriteLine("Den valgte adresse er:"); //ad1.PrintAdress(); //Console.WriteLine("Adressen indeholder følgende personer via ad1.PrintPersons():"); //ad1.PrintPersons(); //Console.WriteLine(); //checkpoint = Console.ReadKey(); //Console.WriteLine(); //Console.WriteLine("Ændring af Jens' efternavn til Larsen."); //Jens.Efternavn("Larsen"); //Console.WriteLine("Adressen indeholder følgende personer via ad1.PrintPersons():"); //ad1.PrintPersons(); //checkpoint = Console.ReadKey(); //Console.WriteLine(); //Console.WriteLine("Følgende er printet fra joint klassen: "); //foreach (var VARIABLE in JoinPersonAdresse.PersonAdresses) //{ // VARIABLE.Print(); // Console.WriteLine(); //} //checkpoint = Console.ReadKey(); using (var db = new PersonsContext()) { // adding entities //db. add //db. savechanges //var query ByPostnummer aarhus = new ByPostnummer(8000, "Aarhus", "Denmark"); //Console.WriteLine("Indeholder 0 adresser:"); Adresse ad1 = new Adresse("Silkeborgvej", 22, aarhus, "Hjem"); Adresse ad2 = new Adresse("Silkeborgvej", 23, aarhus, "Hjem"); Adresse ad3 = new Adresse("Silkeborgvej", 24, aarhus, "Hjem"); Adresse ad4 = new Adresse("Silkeborgvej", 25, aarhus, "Hjem"); TelefonNummer JensArbejd = new TelefonNummer(12345678, "Arbejd", "Telia"); Person Jens = new Person(11111900, "Jens", "Poulsen", JensArbejd, "Kollega", ad1, "*****@*****.**", "Peter"); db.ByPostnummers.Add(aarhus); db.Adresses.Add(ad1); db.Adresses.Add(ad2); db.Adresses.Add(ad3); db.Adresses.Add(ad4); db.TelefonNummers.Add(JensArbejd); db.Persons.Add(Jens); db.SaveChanges(); var query = from b in db.Adresses select b; foreach (var adress in query) { Console.WriteLine("value: {0}", adress.Persons); } } }
public void Add(Person person) { _context.Persons.Add(person); _context.SaveChanges(); }
public void TearDown() { using var db = new PersonsContext(Constants.ConnectionString); db.Persons.Clear(); db.SaveChanges(); }
public void Add(persons entity) { _personsContext.persons.Add(entity); _personsContext.SaveChanges(); }