MVC and Entity Framework


Published on

My presentation on MVC and Entity Framework and how to get the two to work together.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MVC and Entity Framework

  1. 1. ASP.NET MVC and Entity Framework<br />Utah Code Camp<br />Saturday, September 25, 2010<br />James Johnson<br />Technical Evangelist<br />
  2. 2. Technical Evangelist with ComponentOne<br />Founder and President of the Inland Empire .NET User’s Group<br />Microsoft MVP<br />But I don’t consider myself an expert. I just love to play<br />ADHD/ADD/OCD when it comes to new technology<br />Can’t stay away from the shiny new stuff<br />Please don’t drop any new coins during the presentation<br />Who am I?<br />
  3. 3. Overview of ASP.NET MVC<br />Overview of Entity Framework<br />Things that are cool<br />Things to watch out for<br />How to do it<br />Agenda<br />
  4. 4. Demo<br />
  5. 5. Models<br />Views<br />Controllers<br />No Postbacks<br />Very limited use of existing server controls<br />Clean HTML makes CSS and JavaScript easier<br />What all the cool kids are using these days.<br />ASP.NET MVC<br />
  6. 6. First version (V 1) came with .NET 3.5 SP1 <br />August 2008<br />Not widely thought of by the community<br />Second version (V4) released with .NET 4<br />Maps POCO objects to Database objects<br />A collection of things instead of a dataset of rows<br />“things” are the Entities<br />Entity Framework<br />
  7. 7. Why?<br />Adds a layer of abstraction between Database and Code<br />DBA can structure DB how they want<br />Developer can map to the DB how they want<br />Rename Entities for more comfortable use.<br />EF handles the mapping<br />Entity Framework<br />
  8. 8. Entity Data Model – EDM<br />Deals with the Entities and the Relationships they use<br />Entities<br />Instance of EntityType<br />Represent individual instances of the objects<br />Customer, books, shoes<br />Fully typed<br />Relationships<br />V1 was difficult to work with relationships<br />Needed special tricks to load related data<br />Entity FrameworkDefinitions<br />
  9. 9. Adding an EDM to your project<br />Demo<br />
  10. 10. <ul><li>A design pattern to defer initialization until needed.
  11. 11. EF 4 fixes a lot of problems with this
  12. 12. Supports Lazy Loading
  13. 13. OFF by default
  14. 14. ObjectContext setting, not application setting</li></ul>context.ContextOptions.DeferredLoadingEnabled=true;<br />List<Thing> things = context.Things.ToList();<br />foreach(var thing in things)<br />{<br />varthingItems = thing.ThingItems<br />}<br />Entity FrameworkLazy Loading<br />
  15. 15. Use if you will be needing every related entity<br />List<Thing> things = context.Things.Include(“ThingItems”);<br />foreach(var thing in things)<br />{<br />varthingItems = thing.ThingItems<br />}<br />Entity FrameworkEager Loading<br />
  16. 16. The context is the instance of the entity<br />Passing an entity around to tiers breaks the context<br />V4 handles this issue with “self-tracking” entities<br />Make sure the context is always the same<br />Entity FrameworkContexts<br />
  17. 17. Entity FrameworkContexts<br />public class ModelHelper<br /> {<br /> private static CourseEntities _db;<br /> public static CourseEntitiesCourseEntities<br /> {<br /> get<br /> {<br /> if(_db == null)<br /> _db = new CourseEntities();<br /> return _db;<br /> }<br /> set { _db = value; }<br /> }<br /> }<br /> private readonlyCourseEntities _db = new CourseEntities();<br />
  18. 18. Entity FrameworkContexts<br />private Student AddStudent(Student student, Course course)<br />{<br />student.Courses.Add(course);<br />_db.SaveChanges();<br />}<br />Didn’t work because course was in a different context<br />private Student AddStudent(Student student, Course course)<br />{<br />varnewStudent = GetStudent(student.Id);<br />varnewCourse = GetCourse(course.Id);<br />newStudent.Courses.Add(newCourse);<br /> _db.SaveChanges();<br />}<br />
  19. 19. Very similar to LINQ to SQL<br />Major difference<br />LINQ to SQL - .SingleOrDefault()<br />LINQ to Entities - .FirstOrDefault()<br />Selecting<br />public Course GetCourse(int id)<br />{<br />var course = (from c in _db.Courses<br /> where c.Id.Equals(id)<br /> select c).FirstOrDefault();<br /> return course;<br />}<br />Entity FrameworkLINQ to Entities<br />
  20. 20. Deleting<br />public void DeleteCourse(Course course)<br />{<br /> _db.DeleteObject(course);<br /> _db.SaveChanges();<br />}<br />Adding (Inserting)<br />public void AddCourse(Course course)<br />{<br /> _db.AddToCourses(course); //this will be a list of AddToX<br /> _db.SaveChanges();<br />}<br />Entity FrameworkLINQ to Entities<br />
  21. 21. Editing (Updating)<br />public void EditCourse(Course course)<br />{<br /> _db.Courses.Attach(new Course { Id = course.Id });<br /> _db.Courses.ApplyCurrentValues(course);<br /> _db.SaveChanges();<br />}<br />“course” has been edited somewhere else – MVC Controller, so a “stand-in” is created<br />Entity FrameworkLINQ to Entities<br />
  22. 22. Questions?<br />
  23. 23. Tweet “@componentone <3’s Utah Code Camp” for a second chance to win<br />
  24. 24. James Johnson<br /><br /><br />Twitter, @latringo<br />Inland Empire .NET User’s Group<br /><br />2nd Tuesday’s of each month in Riverside<br />Thank you<br />