Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace JenkinsWeb.Data.Models
- {
- public class PersonDAO
- {
- private static ContactDBEntities context = new ContactDBEntities();
- public DbSet<Person> GetPersonList()
- {
- return context.People;
- }
- public Person AddPerson(Person person)
- {
- var p = context.People.Add(person);
- context.SaveChanges();
- return p;
- }
- public Person ManagePerson(Person person, EntityState state)
- {
- var p = context.Entry(person);
- p.State = state;
- context.SaveChanges();
- return p.Entity;
- }
- public Person FindPerson(Person person)
- {
- //context.People.Find() //find everything with this rows and columns. DBset is part of icollection adn that is part of IENumerable.
- return context.People.Where(p => p.FirstName == person.FirstName).OrderBy(c => c.LastName).ToArray()[0]; //Where grabs a function
- }
- public Person FindPerson2(Person person) //this is linq also is lazy loaded
- {
- var query = (from p in context.People
- where p.FirstName == person.FirstName
- orderby p.LastName
- select p);
- AddPerson(person);
- return query.FirstOrDefault(); //Doesnt run the query until here and the updated version will be called. Lazy Execution.
- }
- public object JoinPerson2(Person person) //this is linq also is lazy loaded
- {
- var query = from p in context.People
- join cp in context.People on p.FirstName equals cp.LastName
- select (new { p.PersonId, cp.Active }); //anonomous object - object of no type.
- return query.ElementAt(0);
- }
- //link cannot do insert
- public void IncludePerson(Person person)
- {
- var q = context.People.Include("PersonRole"); //Include is eager.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement