Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Linq;
- public class DataBaseHandler
- {
- // some usages to return objects : http://stackoverflow.com/questions/3485317/entity-framework-4-single-vs-first-vs-firstordefault
- public static void InsertPerson(Person person)
- { // yup the context is disposable !
- using (PersonDbContext ctx = new PersonDbContext())
- {
- ctx.Persons.Add(person);
- ctx.SaveChanges();
- // Yup thats it :) EZ isn't it
- }
- }
- public static void EditPerson(Person person)
- { // yup the context is disposable !
- using (PersonDbContext ctx = new PersonDbContext())
- {
- // first we get the person matching the one we are looking for
- Person getPerson = ctx.Persons.Where(p => p.PersonID == person.PersonID).Single();
- // assing the changed values
- getPerson.Firstname = person.Firstname;
- getPerson.Lastname = person.Lastname;
- getPerson.Birthdate = person.Birthdate;
- ctx.SaveChanges();
- }
- }
- public static Person GetPerson(string personName)
- { // yup the context is disposable !
- using (PersonDbContext ctx = new PersonDbContext())
- {
- // check if we have values in our table Persons
- if (ctx.Persons.Any())
- {
- return ctx.Persons.Where(p => p.Firstname == personName).SingleOrDefault();
- }
- return null;
- }
- }
- public static void RemovePerson(Person person)
- { // yup the context is disposable !
- using (PersonDbContext ctx = new PersonDbContext())
- {
- if (ctx.Persons.Any())
- {
- Person getPerson = ctx.Persons.Where(p => p.PersonID == person.PersonID).Single();
- ctx.Persons.Remove(getPerson);
- ctx.SaveChanges();
- }
- }
- }
- public static List<Person> GetAllPersons()
- { // yup the context is disposable !
- using (PersonDbContext ctx = new PersonDbContext())
- {
- // check if we have values in our table Persons
- if (ctx.Persons.Any())
- {
- return ctx.Persons.ToList();
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement