Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace JenkinsWeb.Data.Models
  8. {
  9. public class PersonDAO
  10. {
  11. private static ContactDBEntities context = new ContactDBEntities();
  12. public DbSet<Person> GetPersonList()
  13. {
  14. return context.People;
  15. }
  16. public Person AddPerson(Person person)
  17. {
  18. var p = context.People.Add(person);
  19. context.SaveChanges();
  20. return p;
  21. }
  22. public Person ManagePerson(Person person, EntityState state)
  23. {
  24. var p = context.Entry(person);
  25. p.State = state;
  26. context.SaveChanges();
  27. return p.Entity;
  28. }
  29.  
  30. public Person FindPerson(Person person)
  31. {
  32. //context.People.Find() //find everything with this rows and columns. DBset is part of icollection adn that is part of IENumerable.
  33.  
  34. return context.People.Where(p => p.FirstName == person.FirstName).OrderBy(c => c.LastName).ToArray()[0]; //Where grabs a function
  35. }
  36.  
  37. public Person FindPerson2(Person person) //this is linq also is lazy loaded
  38. {
  39. var query = (from p in context.People
  40. where p.FirstName == person.FirstName
  41. orderby p.LastName
  42. select p);
  43.  
  44. AddPerson(person);
  45.  
  46. return query.FirstOrDefault(); //Doesnt run the query until here and the updated version will be called. Lazy Execution.
  47. }
  48.  
  49.  
  50. public object JoinPerson2(Person person) //this is linq also is lazy loaded
  51. {
  52. var query = from p in context.People
  53. join cp in context.People on p.FirstName equals cp.LastName
  54. select (new { p.PersonId, cp.Active }); //anonomous object - object of no type.
  55.  
  56. return query.ElementAt(0);
  57. }
  58.  
  59. //link cannot do insert
  60.  
  61. public void IncludePerson(Person person)
  62. {
  63. var q = context.People.Include("PersonRole"); //Include is eager.
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement