Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

MVC and Entity Framework 4



Similar to Silicon Valley presentation with a few extra slides relating to Repositories

Similar to Silicon Valley presentation with a few extra slides relating to Repositories



Total Views
Views on SlideShare
Embed Views



1 Embed 1

http://speakerrate.com 1



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    MVC and Entity Framework 4 MVC and Entity Framework 4 Presentation Transcript

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