public IHttpActionResult PutPerson(int id, Person person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != person.PersonId) { 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 City Create(string name, Country country) { City city = new City(name, country); _personDbContext.Cities.Add(city); _personDbContext.SaveChanges(); return city; }
public Person Create(string name, City city, string phone) { Person person = new Person(name, city, phone); _personDbContext.Persons.Add(person); _personDbContext.SaveChanges(); return(person); }
public T Add(T o) { var entity = dbContext.Set <T>().Create(); dbContext.Set <T>().Add(o); dbContext.SaveChanges(); return(entity); }
public Country Create(string name) { Country country = new Country(name); _personDbContext.Countries.Add(country); _personDbContext.SaveChanges(); return(country); }
public void Remove(int id) { var result = _context.Persons.ToList().SingleOrDefault(x => x.Id == id); if (result == null) { return; } else { _context.Persons.Remove(result); _context.SaveChanges(); } }
public IActionResult savePerson([FromBody] Person person) { try { _context.Persons.Add(person); // Saves changes _context.SaveChanges(); return(Ok(person)); } catch (Exception ex) { Console.WriteLine("Excepcion ... " + ex.ToString()); return(BadRequest("Error")); } }
public void AddAccessToPerson(DateTime AccessFrom, DateTime AccessTo, string personId) { if (personDbContext.People.FirstOrDefault(x => x.id == personId).accessibilities == null) { personDbContext.People.FirstOrDefault(x => x.id == personId).accessibilities = new List <Accessibility>(); } personDbContext.People .Include(x => x.accessibilities) .FirstOrDefault(x => x.id == personId).accessibilities.Add(new Accessibility() { AccessFrom = AccessFrom, AccessTo = AccessTo }); personDbContext.SaveChanges(); }
static void Main(string[] args) { try { ManageDatabse(); using (var ctx = new PersonDbContext()) { var person = new Person() { PersonName = "Sonam", Age = 43, Gender = "Female" }; ctx.Persons.Add(person); ctx.SaveChanges(); Console.WriteLine("Added Person"); foreach (var p in ctx.Persons.ToList()) { Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Age} {p.Gender}"); } } } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); }
static void Main(string[] args) { try { ManageDatabase(); using (var ctx = new PersonDbContext()) { var Person = new Person() { PersonName = "Abhijeet", Gender = "Male", Age = 28 }; ctx.Persons.Add(Person); ctx.SaveChanges(); Console.WriteLine("Added Person"); foreach (var p in ctx.Persons.ToList()) { //$ use as interpolation no need to do + p.Personid+p.PersoName Console.WriteLine($"{p.PersonId}{p.PersonName}{p.Age}{p.Gender}"); } } } catch (Exception ex) { Console.WriteLine($"{ex.Message}{ex.InnerException}"); } Console.ReadLine(); }
static void Main(string[] args) { Producer producer = new Producer(); Consumer consumer = new Consumer(); Console.WriteLine("Enter person name"); string username = Console.ReadLine(); using (var db = new PersonDbContext()) { var person = new Person { Name = username }; db.persons.Add(person); db.SaveChanges(); producer.produceMesaage(person); } consumer.consumeMessages(); Console.ReadLine(); }
public bool CreateLocation(string MyNewLocation) { using (PersonDbContext context = new PersonDbContext()) { var DataLocations = context.Locations; var ExistsQuary = DataLocations.FirstOrDefault(p => p.LocationName == MyNewLocation); if (ExistsQuary == null) { Locations MyNewLocationData = new Locations() { LocationId = DataLocations.Max(p => p.LocationId) + 1, LocationName = MyNewLocation }; DataLocations.Add(MyNewLocationData); try { context.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine(e); } } return(false); } }
static void Main(string[] args) { try { MonitorDb(); var per = new Person() { PersonId = 1001, PersonName = "Mahesh Sabnis", Address = "Bavdhan" }; // set values for the private members by calling the public methods per.SetContatcNo(992325); per.SetIncome(120000); using (var ctx = new PersonDbContext()) { ctx.Persons.Add(per); ctx.SaveChanges(); // read the data foreach (var p in ctx.Persons.ToList()) { Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Address} {p.GetContactNo()} {p.GetIncome()}"); } } } catch (Exception ex) { Console.WriteLine($"Error Occured {ex.Message}"); } Console.ReadLine(); }
static void Main(string[] args) { try { ManageDatabse(); using (var ctx = new PersonDbContext()) { var person = new Person() { //PersonId = 1, this will get created as identity column in db so not required PersonName = "Ketan", Age = 32, Gender = "Male" }; ctx.Persons.Add(person); ctx.SaveChanges(); Console.WriteLine("Added Person"); foreach (var p in ctx.Persons.ToList()) { Console.WriteLine($"{p.PersonId} {p.PersonName} {p.Age} {p.Gender}"); } } } catch (Exception ex) { Console.WriteLine($"{ex.Message} {ex.InnerException}"); } Console.ReadLine(); }
public void DeletePersonById(int personId) { using var db = new PersonDbContext(); var personToDelete = db.Persons.Find(personId); db.Persons.Remove(personToDelete); db.SaveChanges(); }
public void CreatePerson(Person person) { using (var db = new PersonDbContext()) { db.Persons.Add(person); db.SaveChanges(); } }
public Person ChangePassword(int personIdPasswordChange, Person password) { using var db = new PersonDbContext(); var newPassword = db.Persons.First(person => person.Id == personIdPasswordChange); newPassword.Password = password.Password; db.SaveChanges(); return(newPassword); }
// レコードの更新 static void Update(int id, string name) { using (var db = new PersonDbContext()) { var person = db.Persons.Where(x => x.Id == id).FirstOrDefault(); person.Name = name; db.SaveChanges(); } }
// レコードの削除 static void Delete(int id) { using (var db = new PersonDbContext()) { var person = db.Persons.Where(x => x.Id == id).FirstOrDefault(); db.Persons.Remove(person); db.SaveChanges(); } }
public ActionResult Create(Person person) { try { if (ModelState.IsValid) { PersonDb.Persons.Add(person); PersonDb.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException) { //Log the error (add a variable name after DataException) ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(person)); }
public House CreateHouse(House house) { using (var db = new PersonDbContext()) { // Create db.Houses.Add(house); db.SaveChanges(); return(house); } }
public Person Login(string email, string password) { using var db = new PersonDbContext(); var login = db.Persons.FirstOrDefault(person => person.Email == email); login.Email = email; //login.Password = password; db.SaveChanges(); return(login); }
public PersonRepository(PersonDbContext context) { if (context.People.Count() == 0) { context.Initialize(); context.SaveChanges(); } this.context = context; }
public ActionResult Create(Person person, int[] selectedSkills) { if (!ModelState.IsValid) { if (ModelState.IsValidField("Name")) { if (selectedSkills != null) { foreach (var skill in db.Skills.Where(sk => selectedSkills.Contains(sk.Id))) { person.Skills.Add(skill); } } db.People.Add(person); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(person)); } else { if (db.People.Where(p => p.PhoneNumber.Equals(person.PhoneNumber)).Any()) { ModelState.AddModelError("PhoneNumber", "this number is already in Database"); return(View("Create")); } if (person.CompanyId == 0) { person.CompanyId = null; } if (selectedSkills != null) { foreach (var skill in db.Skills.Where(sk => selectedSkills.Contains(sk.Id))) { person.Skills.Add(skill); } } db.People.Add(person); db.SaveChanges(); return(RedirectToAction("Index")); } }
private void SeedData() { if (!dbContext.Persons.Any()) { dbContext.Persons.Add(new myData.DTO.Person() { Id = 1, Name = "John Doe", Email = "*****@*****.**" }); dbContext.Persons.Add(new myData.DTO.Person() { Id = 2, Name = "Jane Doe", Email = "*****@*****.**" }); dbContext.Persons.Add(new myData.DTO.Person() { Id = 3, Name = "Tom Smith", Email = "*****@*****.**" }); dbContext.SaveChanges(); } }
public void Init() { using (var db = new PersonDbContext()) { var persons = repository.GetPersons(); db.Persons.AddRange(persons); db.SaveChanges(); Console.WriteLine("Data saved"); } }
public async Task PutPerson() { // Arrange var id = 0; var options = new DbContextOptionsBuilder <PersonDbContext>() .UseInMemoryDatabase(databaseName: "TPSDatabase") .Options; using (var context = new PersonDbContext(options, true)) { if (context.Persons.Where(b => b.FirstName == "John" && b.LastName == "Doe").IsNullOrEmpty()) { context.Persons.Add(new Person { IsActive = true, FirstName = "John", LastName = "Doe", Address = "123 Main St", City = "Anytown", State = "AA", PostalCode = "11111", ImageName = "profile.png", ImageData = null, }); context.SaveChanges(); } id = context.Persons .AsNoTracking() .Where(b => b.FirstName == "John" && b.LastName == "Doe").FirstOrDefault().Id; var peopleController = new PeopleController(new NullLogger <PeopleController>(), context); // Act var result = await peopleController.PutPerson(id, new Person { Id = id, IsActive = true, FirstName = "Jane", LastName = "Doe", Address = "123 Main St", City = "Anytown", State = "AA", PostalCode = "11111", ImageName = "profile.png", ImageData = null, }); // Assert Assert.IsNotNull(context.Persons .AsNoTracking() .Where(b => b.FirstName == "Jane").FirstOrDefault()); } await Task.CompletedTask; }
public void AddPeopleToMemDb() { IEnumerable <Person> people; using (StreamReader file = new StreamReader(loader.GetJsonSource())) { people = Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <Person> >(file.ReadToEnd()); } personContext.People.AddRange(people); personContext.SaveChanges(); }
public Pet ChangeOwner(int currentOwnerId, Pet NewOwnerId) { using (var db = new PersonDbContext()) { var petOwnerToChange = db.Pets.First(pet => pet.Id == currentOwnerId); petOwnerToChange.PersonId = NewOwnerId.PersonId; db.Pets.Update(petOwnerToChange); db.SaveChanges(); return(NewOwnerId); } }
public void CreatePerson() { using (var context = new PersonDbContext()) { var person = new Person { FirstName = "Test", DateOfBirth = DateTime.Today }; context.People.Add(person); context.SaveChanges(); Assert.IsTrue(person.Id != Guid.Empty); } }