SlideShare uma empresa Scribd logo
1 de 20
ASP.NET MVC and Entity Framework Utah Code Camp Saturday, September 25, 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 Overview of Entity Framework Things that are cool Things to watch out for How to do it Agenda
Demo
Models Views Controllers No Postbacks Very limited use of existing server controls Clean HTML makes CSS and JavaScript easier What all the cool kids are using these days. ASP.NET MVC
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 Demo
[object Object]
EF 4 fixes a lot of problems with this
Supports Lazy Loading
OFF by default
ObjectContext setting, not application settingcontext.ContextOptions.DeferredLoadingEnabled=true; 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         {             get             {                 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) { student.Courses.Add(course); _db.SaveChanges(); } 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); newStudent.Courses.Add(newCourse); 	_db.SaveChanges(); }
Very similar to LINQ to SQL Major difference LINQ to SQL - .SingleOrDefault() LINQ to Entities - .FirstOrDefault() Selecting 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) { 	_db.DeleteObject(course);   _db.SaveChanges(); } Adding (Inserting) public void AddCourse(Course course) {    _db.AddToCourses(course); //this will be a list of AddToX    _db.SaveChanges(); } Entity FrameworkLINQ to Entities

Mais conteúdo relacionado

Mais procurados

ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
habib_786
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with Python
Larry Cai
 

Mais procurados (20)

PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
ASP.NET MVC Presentation
ASP.NET MVC PresentationASP.NET MVC Presentation
ASP.NET MVC Presentation
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
REST-API overview / concepts
REST-API overview / conceptsREST-API overview / concepts
REST-API overview / concepts
 
MVC Architecture
MVC ArchitectureMVC Architecture
MVC Architecture
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
ASP.NET Web API
ASP.NET Web APIASP.NET Web API
ASP.NET Web API
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
ASP.NET Basics
ASP.NET Basics ASP.NET Basics
ASP.NET Basics
 
Php functions
Php functionsPhp functions
Php functions
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Sencha ExtJs Learning Part 1 - Layout And Container in Sencha ExtJs - By Irfa...
Sencha ExtJs Learning Part 1 - Layout And Container in Sencha ExtJs - By Irfa...Sencha ExtJs Learning Part 1 - Layout And Container in Sencha ExtJs - By Irfa...
Sencha ExtJs Learning Part 1 - Layout And Container in Sencha ExtJs - By Irfa...
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
 
Learn REST API with Python
Learn REST API with PythonLearn REST API with Python
Learn REST API with Python
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 

Destaque

LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
Akhil Mittal
 
Ling to SQL and Entity Framework performance analysis
Ling to SQL and Entity Framework performance analysisLing to SQL and Entity Framework performance analysis
Ling to SQL and Entity Framework performance analysis
Alexander Konduforov
 
3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC
Mohd Manzoor Ahmed
 

Destaque (17)

Parallel Extentions to the .NET Framework
Parallel Extentions to the .NET FrameworkParallel Extentions to the .NET Framework
Parallel Extentions to the .NET Framework
 
ASP.NET MVC: new era?
ASP.NET MVC: new era?ASP.NET MVC: new era?
ASP.NET MVC: new era?
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Real-time ASP.NET with SignalR
Real-time ASP.NET with SignalRReal-time ASP.NET with SignalR
Real-time ASP.NET with SignalR
 
ASP.NET MVC and Entity Framework 4
ASP.NET MVC and Entity Framework 4ASP.NET MVC and Entity Framework 4
ASP.NET MVC and Entity Framework 4
 
Ling to SQL and Entity Framework performance analysis
Ling to SQL and Entity Framework performance analysisLing to SQL and Entity Framework performance analysis
Ling to SQL and Entity Framework performance analysis
 
Building a MVC eCommerce Site in Under 5 Minutes
Building a MVC eCommerce Site in Under 5 MinutesBuilding a MVC eCommerce Site in Under 5 Minutes
Building a MVC eCommerce Site in Under 5 Minutes
 
Introducing LINQ
Introducing LINQIntroducing LINQ
Introducing LINQ
 
.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ.NET,ASP .NET, Angular Js,LinQ
.NET,ASP .NET, Angular Js,LinQ
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use it
 
Dotnet Frameworks Version History
Dotnet Frameworks Version HistoryDotnet Frameworks Version History
Dotnet Frameworks Version History
 
Introduccion a LINQ
Introduccion a LINQIntroduccion a LINQ
Introduccion a LINQ
 
LINQ
LINQLINQ
LINQ
 
3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC3-TIER ARCHITECTURE IN ASP.NET MVC
3-TIER ARCHITECTURE IN ASP.NET MVC
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 

Semelhante a MVC and Entity Framework

What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
ukdpe
 

Semelhante a MVC and Entity Framework (20)

MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
 
MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Ef Poco And Unit Testing
Ef Poco And Unit TestingEf Poco And Unit Testing
Ef Poco And Unit Testing
 
La sql
La sqlLa sql
La sql
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Poco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core Data
 
Entity Framework 4 In Microsoft Visual Studio 2010
Entity Framework 4 In Microsoft Visual Studio 2010Entity Framework 4 In Microsoft Visual Studio 2010
Entity Framework 4 In Microsoft Visual Studio 2010
 
An Overview of Entity Framework
An Overview of Entity FrameworkAn Overview of Entity Framework
An Overview of Entity Framework
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
 
Intake 37 ef2
Intake 37 ef2Intake 37 ef2
Intake 37 ef2
 
Entity Framework Today (May 2012)
Entity Framework Today (May 2012)Entity Framework Today (May 2012)
Entity Framework Today (May 2012)
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

MVC and Entity Framework

  • 1. ASP.NET MVC and Entity Framework Utah Code Camp Saturday, September 25, 2010 James Johnson Technical Evangelist
  • 2. 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?
  • 3. Overview of ASP.NET MVC Overview of Entity Framework Things that are cool Things to watch out for How to do it Agenda
  • 5. Models Views Controllers No Postbacks Very limited use of existing server controls Clean HTML makes CSS and JavaScript easier What all the cool kids are using these days. ASP.NET MVC
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Adding an EDM to your project Demo
  • 10.
  • 11. EF 4 fixes a lot of problems with this
  • 14. ObjectContext setting, not application settingcontext.ContextOptions.DeferredLoadingEnabled=true; List<Thing> things = context.Things.ToList(); foreach(var thing in things) { varthingItems = thing.ThingItems } Entity FrameworkLazy Loading
  • 15. 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
  • 16. 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
  • 17. Entity FrameworkContexts public class ModelHelper { private static CourseEntities _db; public static CourseEntitiesCourseEntities { get { if(_db == null) _db = new CourseEntities(); return _db; } set { _db = value; } } } private readonlyCourseEntities _db = new CourseEntities();
  • 18. Entity FrameworkContexts private Student AddStudent(Student student, Course course) { student.Courses.Add(course); _db.SaveChanges(); } 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); newStudent.Courses.Add(newCourse); _db.SaveChanges(); }
  • 19. Very similar to LINQ to SQL Major difference LINQ to SQL - .SingleOrDefault() LINQ to Entities - .FirstOrDefault() Selecting 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
  • 20. Deleting public void DeleteCourse(Course course) { _db.DeleteObject(course); _db.SaveChanges(); } Adding (Inserting) public void AddCourse(Course course) { _db.AddToCourses(course); //this will be a list of AddToX _db.SaveChanges(); } Entity FrameworkLINQ to Entities
  • 21. Editing (Updating) public void EditCourse(Course course) { _db.Courses.Attach(new Course { Id = course.Id }); _db.Courses.ApplyCurrentValues(course); _db.SaveChanges(); } “course” has been edited somewhere else – MVC Controller, so a “stand-in” is created Entity FrameworkLINQ to Entities
  • 23. Tweet “@componentone <3’s Utah Code Camp” for a second chance to win
  • 24. James Johnson jamesj@componentone.com www.latringo.me Twitter, @latringo Inland Empire .NET User’s Group www.iedotnetug.org 2nd Tuesday’s of each month in Riverside Thank you