SlideShare a Scribd company logo
1 of 22
ASP.NET MVC and Entity Framework San Diego .NET User Group Tuesday, March 23, 2010 James Johnson
Founder and President of the Inland Empire .NET User’s Group Independent Consultant 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 shiny coins during the presentation Who am I?
An effort to help support local user groups by providing speakers Telerik speakers bureau
Overview of ASP.NET MVC Overview of Entity Framework Things that are cool Things to watch out for How to do it Agenda
Demo
V 2.0 RTM’d March 11, 2010 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
Hey! There’s a big honking server control there! Using the TelerikRadEditor in this project Very easy to setup if you follow these rules Needs to be in a “form” tag, not Html.BeginForm() Handlers need to be changed from .aspx to .axd DialogHandlerUrl, SpellCheckChettings Use a custom tools file ASP.NET MVCTelerikRadEditor
Setting <% RadEditor1.Content = Model.Description %> Getting Need to pass in the request and name of editor ASP.NET MVCTelerikRadEditor Set and Get Content varcontent = _radControlHelper.GetEditorContent(Request, “RadEditor1"); public string GetEditorContent(HttpRequestBase request, string editorId) { varrawContent = (from postedValue in request.Form.Keys.OfType<string>()                               where postedValue.EndsWith(editorId)                               select request.Form[postedValue]).FirstOrDefault(); return Telerik.Web.UI.Editor.ContentEncoder.Decode(rawContent); }
First version (V 1) came with .NET 3.5 SP1  August 2008 Not widely thought of by the community Second version (V4) is set to be 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
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 context.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 will handle 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
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
Questions?
James Johnson james@iedotnetug.org www.duringlunch.com Twitter, @latringo Inland Empire .NET User’s Group www.iedotnetug.org 2nd Tuesday’s of each month in Riverside Thank you

More Related Content

What's hot

Java day2016 "Reinventing design patterns with java 8"
Java day2016 "Reinventing design patterns with java 8"Java day2016 "Reinventing design patterns with java 8"
Java day2016 "Reinventing design patterns with java 8"Alexander Pashynskiy
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its managementVinay Kumar Pulabaigari
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & AnswerMildain Solutions
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code FirstJames Johnson
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)ejlp12
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptBill Buchan
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...Marco Tusa
 
Spring Test DBUnit
Spring Test DBUnitSpring Test DBUnit
Spring Test DBUnitJaran Flaath
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaJignesh Aakoliya
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)Craig Dickson
 
ObjectCreatorUserGuide
ObjectCreatorUserGuideObjectCreatorUserGuide
ObjectCreatorUserGuideRoger Robins
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Nelson Gomes
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)Fad Zulkifli
 
Dropping content isn't a drag!
Dropping content isn't a drag!Dropping content isn't a drag!
Dropping content isn't a drag!XMetaL
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced FunctionsWebStackAcademy
 

What's hot (20)

Java day2016 "Reinventing design patterns with java 8"
Java day2016 "Reinventing design patterns with java 8"Java day2016 "Reinventing design patterns with java 8"
Java day2016 "Reinventing design patterns with java 8"
 
Object identification and its management
Object identification and its managementObject identification and its management
Object identification and its management
 
React JS Interview Question & Answer
React JS Interview Question & AnswerReact JS Interview Question & Answer
React JS Interview Question & Answer
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)Introduction to JPA (JPA version 2.0)
Introduction to JPA (JPA version 2.0)
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
Accessing Data Through Hibernate; What DBAs Should Tell Developers and Vice V...
 
Spring Test DBUnit
Spring Test DBUnitSpring Test DBUnit
Spring Test DBUnit
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company india
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
ObjectCreatorUserGuide
ObjectCreatorUserGuideObjectCreatorUserGuide
ObjectCreatorUserGuide
 
Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.Codebits 2012 - Fast relational web site construction.
Codebits 2012 - Fast relational web site construction.
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)
 
Dropping content isn't a drag!
Dropping content isn't a drag!Dropping content isn't a drag!
Dropping content isn't a drag!
 
JavaScript - Chapter 7 - Advanced Functions
 JavaScript - Chapter 7 - Advanced Functions JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 7 - Advanced Functions
 

Viewers also liked

Buzzmedia Octagon Keynote
Buzzmedia Octagon KeynoteBuzzmedia Octagon Keynote
Buzzmedia Octagon KeynoteChris George
 
Прикладной коннективизм
Прикладной коннективизмПрикладной коннективизм
Прикладной коннективизмIvan Travkin
 
Skema Bm Kertas2 Set2
Skema Bm Kertas2 Set2Skema Bm Kertas2 Set2
Skema Bm Kertas2 Set2Kay Aniza
 
Marketing Communications Spring 2008 Final Project
Marketing Communications Spring 2008 Final ProjectMarketing Communications Spring 2008 Final Project
Marketing Communications Spring 2008 Final Projectlat886
 
Twitter for Journalists Utrecht june 11 2009
Twitter for Journalists Utrecht june 11 2009Twitter for Journalists Utrecht june 11 2009
Twitter for Journalists Utrecht june 11 2009Bart Brouwers
 
Amphibians.d (1)
Amphibians.d (1)Amphibians.d (1)
Amphibians.d (1)Isabel Rojo
 
Jeff Martin: Portfolio
Jeff Martin: PortfolioJeff Martin: Portfolio
Jeff Martin: PortfolioJeff Martin
 
A Historia
A HistoriaA Historia
A Historiacurradbc
 
Informationliteracy
InformationliteracyInformationliteracy
InformationliteracyIreubzaet
 
Plazacubiertaweb
PlazacubiertawebPlazacubiertaweb
Plazacubiertawebraqueleta
 
Creatively Common (#BootCamp12)
Creatively Common (#BootCamp12)Creatively Common (#BootCamp12)
Creatively Common (#BootCamp12)Michael Fienen
 
The Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorThe Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorAbhranil Das
 
A Search For Compassion
A Search For CompassionA Search For Compassion
A Search For CompassionMichelle
 

Viewers also liked (20)

Buzzmedia Octagon Keynote
Buzzmedia Octagon KeynoteBuzzmedia Octagon Keynote
Buzzmedia Octagon Keynote
 
Прикладной коннективизм
Прикладной коннективизмПрикладной коннективизм
Прикладной коннективизм
 
Skema Bm Kertas2 Set2
Skema Bm Kertas2 Set2Skema Bm Kertas2 Set2
Skema Bm Kertas2 Set2
 
Marketing Communications Spring 2008 Final Project
Marketing Communications Spring 2008 Final ProjectMarketing Communications Spring 2008 Final Project
Marketing Communications Spring 2008 Final Project
 
Twitter for Journalists Utrecht june 11 2009
Twitter for Journalists Utrecht june 11 2009Twitter for Journalists Utrecht june 11 2009
Twitter for Journalists Utrecht june 11 2009
 
ADHD
ADHDADHD
ADHD
 
Amphibians.d (1)
Amphibians.d (1)Amphibians.d (1)
Amphibians.d (1)
 
Amphibians
AmphibiansAmphibians
Amphibians
 
One 2 One
One 2  OneOne 2  One
One 2 One
 
Texas Instruments
Texas InstrumentsTexas Instruments
Texas Instruments
 
Jeff Martin: Portfolio
Jeff Martin: PortfolioJeff Martin: Portfolio
Jeff Martin: Portfolio
 
A Historia
A HistoriaA Historia
A Historia
 
Informationliteracy
InformationliteracyInformationliteracy
Informationliteracy
 
E Learners Presentation
E Learners PresentationE Learners Presentation
E Learners Presentation
 
Plazacubiertaweb
PlazacubiertawebPlazacubiertaweb
Plazacubiertaweb
 
It Idea
It IdeaIt Idea
It Idea
 
Soci2
Soci2Soci2
Soci2
 
Creatively Common (#BootCamp12)
Creatively Common (#BootCamp12)Creatively Common (#BootCamp12)
Creatively Common (#BootCamp12)
 
The Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorThe Moore-Spiegel Oscillator
The Moore-Spiegel Oscillator
 
A Search For Compassion
A Search For CompassionA Search For Compassion
A Search For Compassion
 

Similar to MVC and Entity Framework 4

MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity FrameworkJames Johnson
 
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 4James Johnson
 
MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4James Johnson
 
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)David McCarter
 
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)David McCarter
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?ukdpe
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkAkhil Mittal
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Svetlin Nakov
 
Entity frameworks101
Entity frameworks101Entity frameworks101
Entity frameworks101Rich Helton
 
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 TopLinkBill Lyons
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core DataMake School
 
MVC Application using EntityFramework Code-First approach Part4
MVC Application using EntityFramework Code-First approach Part4MVC Application using EntityFramework Code-First approach Part4
MVC Application using EntityFramework Code-First approach Part4Akhil Mittal
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best PracticesAndri Yadi
 
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 2010Eric Nelson
 
Learn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database FirstLearn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database FirstJibran Rasheed Khan
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Bill Buchan
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9google
 

Similar to MVC and Entity Framework 4 (20)

MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 
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
 
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
 
La sql
La sqlLa sql
La sql
 
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)
 
What's New for Data?
What's New for Data?What's New for Data?
What's New for Data?
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015
 
Entity frameworks101
Entity frameworks101Entity frameworks101
Entity frameworks101
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
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
 
Intro to Core Data
Intro to Core DataIntro to Core Data
Intro to Core Data
 
MVC Application using EntityFramework Code-First approach Part4
MVC Application using EntityFramework Code-First approach Part4MVC Application using EntityFramework Code-First approach Part4
MVC Application using EntityFramework Code-First approach Part4
 
Entity Framework v2 Best Practices
Entity Framework v2 Best PracticesEntity Framework v2 Best Practices
Entity Framework v2 Best Practices
 
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
 
Learn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database FirstLearn Entity Framework in a day with Code First, Model First and Database First
Learn Entity Framework in a day with Code First, Model First and Database First
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
 

Recently uploaded

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 FMESafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 SavingEdi Saputra
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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 Takeoffsammart93
 

Recently uploaded (20)

+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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 

MVC and Entity Framework 4

  • 1. ASP.NET MVC and Entity Framework San Diego .NET User Group Tuesday, March 23, 2010 James Johnson
  • 2. Founder and President of the Inland Empire .NET User’s Group Independent Consultant 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 shiny coins during the presentation Who am I?
  • 3. An effort to help support local user groups by providing speakers Telerik speakers bureau
  • 4. Overview of ASP.NET MVC Overview of Entity Framework Things that are cool Things to watch out for How to do it Agenda
  • 6. V 2.0 RTM’d March 11, 2010 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
  • 7. Hey! There’s a big honking server control there! Using the TelerikRadEditor in this project Very easy to setup if you follow these rules Needs to be in a “form” tag, not Html.BeginForm() Handlers need to be changed from .aspx to .axd DialogHandlerUrl, SpellCheckChettings Use a custom tools file ASP.NET MVCTelerikRadEditor
  • 8. Setting <% RadEditor1.Content = Model.Description %> Getting Need to pass in the request and name of editor ASP.NET MVCTelerikRadEditor Set and Get Content varcontent = _radControlHelper.GetEditorContent(Request, “RadEditor1"); public string GetEditorContent(HttpRequestBase request, string editorId) { varrawContent = (from postedValue in request.Form.Keys.OfType<string>() where postedValue.EndsWith(editorId) select request.Form[postedValue]).FirstOrDefault(); return Telerik.Web.UI.Editor.ContentEncoder.Decode(rawContent); }
  • 9. First version (V 1) came with .NET 3.5 SP1 August 2008 Not widely thought of by the community Second version (V4) is set to be 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
  • 10. 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
  • 11. 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
  • 12. Adding an EDM to your project Demo
  • 13. 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 context.ContextOptions.DeferredLoadingEnabled=true; List<Thing> things = context.Things.ToList(); foreach(var thing in things) { varthingItems = thing.ThingItems } Entity FrameworkLazy Loading
  • 14. 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
  • 15. The context is the instance of the entity Passing an entity around to tiers breaks the context V4 will handle this issue with “self-tracking” entities Make sure the context is always the same Entity FrameworkContexts
  • 16. 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();
  • 17. 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(); }
  • 18. 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
  • 19. 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
  • 20. 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
  • 22. James Johnson james@iedotnetug.org www.duringlunch.com Twitter, @latringo Inland Empire .NET User’s Group www.iedotnetug.org 2nd Tuesday’s of each month in Riverside Thank you