MVC and Entity Framework 4

3,331 views
3,279 views

Published on

Slide deck presented to the San Diego .NET User Group on Tuesday, March 23, 2010

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,331
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
39
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MVC and Entity Framework 4

  1. 1. ASP.NET MVC and Entity Framework<br />San Diego .NET User Group<br />Tuesday, March 23, 2010<br />James Johnson<br />
  2. 2. Founder and President of the Inland Empire .NET User’s Group<br />Independent Consultant<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 shiny coins during the presentation<br />Who am I?<br />
  3. 3. An effort to help support local user groups by providing speakers<br />Telerik speakers bureau<br />
  4. 4. 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 />
  5. 5. Demo<br />
  6. 6. V 2.0 RTM’d March 11, 2010<br />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 />
  7. 7. Hey! There’s a big honking server control there!<br />Using the TelerikRadEditor in this project<br />Very easy to setup if you follow these rules<br />Needs to be in a “form” tag, not Html.BeginForm()<br />Handlers need to be changed from .aspx to .axd<br />DialogHandlerUrl, SpellCheckChettings<br />Use a custom tools file<br />ASP.NET MVCTelerikRadEditor<br />
  8. 8. Setting<br /><% RadEditor1.Content = Model.Description %><br />Getting<br />Need to pass in the request and name of editor<br />ASP.NET MVCTelerikRadEditor Set and Get Content<br />varcontent = _radControlHelper.GetEditorContent(Request, “RadEditor1");<br />public string GetEditorContent(HttpRequestBase request, string editorId)<br />{<br />varrawContent = (from postedValue in request.Form.Keys.OfType<string>()<br /> where postedValue.EndsWith(editorId)<br /> select request.Form[postedValue]).FirstOrDefault();<br />return Telerik.Web.UI.Editor.ContentEncoder.Decode(rawContent);<br />}<br />
  9. 9. 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) is set to be 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 />
  10. 10. 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 />
  11. 11. 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 />
  12. 12. Adding an EDM to your project<br />Demo<br />
  13. 13. A design pattern to defer initialization until needed.<br />EF 4 fixes a lot of problems with this<br />Supports Lazy Loading<br />OFF by default<br />ObjectContext setting, not application setting<br />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 />
  14. 14. 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 />
  15. 15. The context is the instance of the entity<br />Passing an entity around to tiers breaks the context<br />V4 will handle this issue with “self-tracking” entities<br />Make sure the context is always the same<br />Entity FrameworkContexts<br />
  16. 16. 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 />
  17. 17. 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 />
  18. 18. 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 />
  19. 19. 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 />
  20. 20. 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 />
  21. 21. Questions?<br />
  22. 22. James Johnson<br />james@iedotnetug.org<br />www.duringlunch.com<br />Twitter, @latringo<br />Inland Empire .NET User’s Group<br />www.iedotnetug.org<br />2nd Tuesday’s of each month in Riverside<br />Thank you<br />

×