Advertisement
Guest User

DataBaseHandler

a guest
Dec 6th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1.  using System.Collections.Generic;
  2.     using System.Linq;
  3.     public class DataBaseHandler
  4.     {
  5.         // some usages to return objects : http://stackoverflow.com/questions/3485317/entity-framework-4-single-vs-first-vs-firstordefault
  6.  
  7.         public static void InsertPerson(Person person)
  8.         {   // yup the context is disposable !
  9.             using (PersonDbContext ctx = new PersonDbContext())
  10.             {
  11.                 ctx.Persons.Add(person);
  12.                 ctx.SaveChanges();
  13.                 // Yup thats it :) EZ isn't it
  14.             }
  15.         }
  16.  
  17.         public static void EditPerson(Person person)
  18.         {   // yup the context is disposable !
  19.             using (PersonDbContext ctx = new PersonDbContext())
  20.             {
  21.                 // first we get the person matching the one we are looking for
  22.                 Person getPerson = ctx.Persons.Where(p => p.PersonID == person.PersonID).Single();
  23.  
  24.                 // assing the changed values
  25.                 getPerson.Firstname = person.Firstname;
  26.                 getPerson.Lastname = person.Lastname;
  27.                 getPerson.Birthdate = person.Birthdate;
  28.  
  29.                 ctx.SaveChanges();
  30.             }
  31.         }
  32.  
  33.         public static Person GetPerson(string personName)
  34.         {   // yup the context is disposable !
  35.             using (PersonDbContext ctx = new PersonDbContext())
  36.             {
  37.                 // check if we have values in our table Persons
  38.                 if (ctx.Persons.Any())
  39.                 {
  40.                     return ctx.Persons.Where(p => p.Firstname == personName).SingleOrDefault();
  41.                 }
  42.                 return null;
  43.             }
  44.         }
  45.  
  46.         public static void RemovePerson(Person person)
  47.         {   // yup the context is disposable !
  48.             using (PersonDbContext ctx = new PersonDbContext())
  49.             {
  50.                 if (ctx.Persons.Any())
  51.                 {
  52.                     Person getPerson = ctx.Persons.Where(p => p.PersonID == person.PersonID).Single();
  53.                     ctx.Persons.Remove(getPerson);
  54.                     ctx.SaveChanges();
  55.                 }
  56.             }
  57.         }
  58.  
  59.         public static List<Person> GetAllPersons()
  60.         {   // yup the context is disposable !
  61.             using (PersonDbContext ctx = new PersonDbContext())
  62.             {
  63.                 // check if we have values in our table Persons
  64.                 if (ctx.Persons.Any())
  65.                 {
  66.                     return ctx.Persons.ToList();
  67.                 }
  68.                 return null;
  69.             }
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement