SlideShare a Scribd company logo
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
Agenda
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.
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 Framework
Definitions
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)
{
var thingItems = thing.ThingItems
}
Entity Framework
Lazy Loading
Use if you will be needing every related entity
List<Thing> things = context.Things.Include(“ThingItems”);
foreach(var thing in things)
{
var thingItems = thing.ThingItems
}
Entity Framework
Eager 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 Framework
Contexts
Entity Framework
Contexts
public class ModelHelper
{
private static CourseEntities _db;
public static CourseEntities CourseEntities
{
get
{
if(_db == null)
_db = new CourseEntities();
return _db;
}
set { _db = value; }
}
}
private readonly CourseEntities _db = new CourseEntities();
Entity Framework
Contexts
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)
{
var newStudent = GetStudent(student.Id);
var newCourse = 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 Framework
LINQ 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 Framework
LINQ 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 Framework
LINQ 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
• stephenwalther.com
• “Download the code” link
Repositories
Repositories
• Add the two projects to your solution
• Add references to your project
using GenericRepository
public class MyController
{
private readonly IGenericRepository _repo;
private readonly CourseEntities _db;
public MyController()
{
_repo = new EFGenericRepository.EFGenericRepository(_db);
}
}
Repositories
// get
_repo.Get<Course>(id);
// edit
_repo.Edit(course);
// create
_repo.Create(course);
// delete
_repo.Delete(course);
// list
var list = _repo.List<Course>().Where(x => x.CourseName.Contains());
Repositories
Questions?
James Johnson
jamesj@componentone.com
www.latringo.me
www.speakerrate.com/latringo
Twitter, @latringo
Inland Empire .NET User’s Group
www.iedotnetug.org
2nd Tuesday’s of each month in Riverside, CA
Thank you

More Related Content

What's hot

Intro to java programming
Intro to java programmingIntro to java programming
Intro to java programmingLeah Stephens
 
Getting Started with Javascript
Getting Started with JavascriptGetting Started with Javascript
Getting Started with Javascript
Akshay Mathur
 
Unit Testing with WOUnit
Unit Testing with WOUnitUnit Testing with WOUnit
Unit Testing with WOUnitWO Community
 
Uklug2012 yellow and blue stream
Uklug2012 yellow and blue streamUklug2012 yellow and blue stream
Uklug2012 yellow and blue stream
Frank van der Linden
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
 
D2W Stateful Controllers
D2W Stateful ControllersD2W Stateful Controllers
D2W Stateful ControllersWO Community
 
PresentationPatterns_v2
PresentationPatterns_v2PresentationPatterns_v2
PresentationPatterns_v2Maksym Tolstik
 
Object Oriented Programing in JavaScript
Object Oriented Programing in JavaScriptObject Oriented Programing in JavaScript
Object Oriented Programing in JavaScript
Akshay Mathur
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
Akshay Mathur
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Viper
ViperViper
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps
Ivano Malavolta
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - Introduction
Senthil Kumar
 
iOS Beginners Lesson 3
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3
Calvin Cheng
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
Oliver Busse
 
React && React Native workshop
React && React Native workshopReact && React Native workshop
React && React Native workshop
Stacy Goh
 
Asp.Net 3.5 Part 2
Asp.Net 3.5 Part 2Asp.Net 3.5 Part 2
Asp.Net 3.5 Part 2
asim78
 
NTBT #1 "Client-Side JavaScript"
NTBT #1 "Client-Side JavaScript"NTBT #1 "Client-Side JavaScript"
NTBT #1 "Client-Side JavaScript"
Frédéric Ghilini
 

What's hot (20)

Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Intro to java programming
Intro to java programmingIntro to java programming
Intro to java programming
 
Getting Started with Javascript
Getting Started with JavascriptGetting Started with Javascript
Getting Started with Javascript
 
Unit Testing with WOUnit
Unit Testing with WOUnitUnit Testing with WOUnit
Unit Testing with WOUnit
 
Uklug2012 yellow and blue stream
Uklug2012 yellow and blue streamUklug2012 yellow and blue stream
Uklug2012 yellow and blue stream
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
D2W Stateful Controllers
D2W Stateful ControllersD2W Stateful Controllers
D2W Stateful Controllers
 
PresentationPatterns_v2
PresentationPatterns_v2PresentationPatterns_v2
PresentationPatterns_v2
 
Object Oriented Programing in JavaScript
Object Oriented Programing in JavaScriptObject Oriented Programing in JavaScript
Object Oriented Programing in JavaScript
 
Getting Started with jQuery
Getting Started with jQueryGetting Started with jQuery
Getting Started with jQuery
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Viper
ViperViper
Viper
 
Life outside WO
Life outside WOLife outside WO
Life outside WO
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps
 
Valentine with Angular js - Introduction
Valentine with Angular js - IntroductionValentine with Angular js - Introduction
Valentine with Angular js - Introduction
 
iOS Beginners Lesson 3
iOS Beginners Lesson 3iOS Beginners Lesson 3
iOS Beginners Lesson 3
 
Utilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino APIUtilizing the OpenNTF Domino API
Utilizing the OpenNTF Domino API
 
React && React Native workshop
React && React Native workshopReact && React Native workshop
React && React Native workshop
 
Asp.Net 3.5 Part 2
Asp.Net 3.5 Part 2Asp.Net 3.5 Part 2
Asp.Net 3.5 Part 2
 
NTBT #1 "Client-Side JavaScript"
NTBT #1 "Client-Side JavaScript"NTBT #1 "Client-Side JavaScript"
NTBT #1 "Client-Side JavaScript"
 

Viewers also liked

SW Development Methodologies
SW Development MethodologiesSW Development Methodologies
SW Development Methodologiesthiago_tadeu
 
Fisio reproduksi pria
Fisio reproduksi priaFisio reproduksi pria
Fisio reproduksi pria
Gara Salvamosa
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
James Johnson
 
Green Eco Powerpoint
Green Eco PowerpointGreen Eco Powerpoint
Green Eco Powerpointcbrady24
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
James Johnson
 

Viewers also liked (8)

Phomenes
PhomenesPhomenes
Phomenes
 
Nz immigration
Nz immigrationNz immigration
Nz immigration
 
SW Development Methodologies
SW Development MethodologiesSW Development Methodologies
SW Development Methodologies
 
Fisio reproduksi pria
Fisio reproduksi priaFisio reproduksi pria
Fisio reproduksi pria
 
Semantics
SemanticsSemantics
Semantics
 
A Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NETA Rich Web experience with jQuery, Ajax and .NET
A Rich Web experience with jQuery, Ajax and .NET
 
Green Eco Powerpoint
Green Eco PowerpointGreen Eco Powerpoint
Green Eco Powerpoint
 
MVC and Entity Framework
MVC and Entity FrameworkMVC and Entity Framework
MVC and Entity Framework
 

Similar to MVC and Entity Framework 4

MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
James Johnson
 
Lerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In AspnetLerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In Aspnet
Julie Lerman
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
Karen Benoit
 
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
Jibran Rasheed Khan
 
04 integrate entityframework
04 integrate entityframework04 integrate entityframework
04 integrate entityframework
Erhwen Kuo
 
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
Eric Nelson
 
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
Stefano Paluello
 
Entity Framework Today (May 2012)
Entity Framework Today (May 2012)Entity Framework Today (May 2012)
Entity Framework Today (May 2012)
Julie Lerman
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO DevsWO Community
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
Gil Fink
 
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
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
Mahmoud Tolba
 
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
 
SharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure FunctionsSharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure Functions
Sébastien Levert
 
Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016
Marco Breveglieri
 
Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
Chris Aniszczyk
 
Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPagesToby Samples
 

Similar to MVC and Entity Framework 4 (20)

MVC and Entity Framework 4
MVC and Entity Framework 4MVC and Entity Framework 4
MVC and Entity Framework 4
 
Lerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In AspnetLerman Adx303 Entity Framework 4 In Aspnet
Lerman Adx303 Entity Framework 4 In Aspnet
 
70487.pdf
70487.pdf70487.pdf
70487.pdf
 
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
 
04 integrate entityframework
04 integrate entityframework04 integrate entityframework
04 integrate entityframework
 
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
 
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
 
Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic Unicorns
 
Entity Framework 4
Entity Framework 4Entity Framework 4
Entity Framework 4
 
Entity Framework Today (May 2012)
Entity Framework Today (May 2012)Entity Framework Today (May 2012)
Entity Framework Today (May 2012)
 
Apache Cayenne for WO Devs
Apache Cayenne for WO DevsApache Cayenne for WO Devs
Apache Cayenne for WO Devs
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
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)
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
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)
 
JS Essence
JS EssenceJS Essence
JS Essence
 
SharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure FunctionsSharePoint Framework, Angular and Azure Functions
SharePoint Framework, Angular and Azure Functions
 
Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016Web Development with Delphi and React - ITDevCon 2016
Web Development with Delphi and React - ITDevCon 2016
 
Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
 
Hibernate in XPages
Hibernate in XPagesHibernate in XPages
Hibernate in XPages
 

Recently uploaded

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

MVC and Entity Framework 4

  • 1. ASP.NET MVC and Entity Framework SoCal Code Camp Saturday, October 23, 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 • Basic MVC Application • Models, Views, Controls, Helpers • Overview of Entity Framework • Things that are cool • Things to watch out for • How to do it Agenda
  • 5. • 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. 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 Framework Definitions
  • 9. Adding an EDM to your project Demo
  • 10. • 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) { var thingItems = thing.ThingItems } Entity Framework Lazy Loading
  • 11. Use if you will be needing every related entity List<Thing> things = context.Things.Include(“ThingItems”); foreach(var thing in things) { var thingItems = thing.ThingItems } Entity Framework Eager Loading
  • 12. • 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 Framework Contexts
  • 13. Entity Framework Contexts public class ModelHelper { private static CourseEntities _db; public static CourseEntities CourseEntities { get { if(_db == null) _db = new CourseEntities(); return _db; } set { _db = value; } } } private readonly CourseEntities _db = new CourseEntities();
  • 14. Entity Framework Contexts 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) { var newStudent = GetStudent(student.Id); var newCourse = GetCourse(course.Id); newStudent.Courses.Add(newCourse); _db.SaveChanges(); }
  • 15. • 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 Framework LINQ to Entities
  • 16. 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 Framework LINQ to Entities
  • 17. 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 Framework LINQ to Entities
  • 18. • 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 • stephenwalther.com • “Download the code” link Repositories
  • 19. Repositories • Add the two projects to your solution • Add references to your project
  • 20. using GenericRepository public class MyController { private readonly IGenericRepository _repo; private readonly CourseEntities _db; public MyController() { _repo = new EFGenericRepository.EFGenericRepository(_db); } } Repositories
  • 21. // get _repo.Get<Course>(id); // edit _repo.Edit(course); // create _repo.Create(course); // delete _repo.Delete(course); // list var list = _repo.List<Course>().Where(x => x.CourseName.Contains()); Repositories
  • 23. James Johnson jamesj@componentone.com www.latringo.me www.speakerrate.com/latringo Twitter, @latringo Inland Empire .NET User’s Group www.iedotnetug.org 2nd Tuesday’s of each month in Riverside, CA Thank you