Entity Framework & LINQ (Language Integrated Query) MIS 324 -- Professor Sandvig 7/26/2019 Entity Framework & LINQ (Language Integrated Query) MIS 324 Professor Sandvig
MIS 324 -- Professor Sandvig 7/26/2019 Outline Entity Framework Model generation Scaffolding Controllers & views LINQ Language integrated query
What is Entity Framework MIS 324 -- Professor Sandvig 7/26/2019 What is Entity Framework Short version: Code-less CRUD web interface Microsoft’s ORM (object relation mapper) Wizards create model, controller, views from database
What is Entity Framework Dapper lightweight ORM Fast Uses standard SQL queries EF is ORM + code writing wizards
EF Model Examines database Example: Generates model of database tables Database with three tables VS: Add New Item ADO.NET Entity Data Model
EF Controller & Views VS: Add > New Scaffolded Item Generates: Controller with CRUD methods Views for CRUD
EF Controller & Views Run Scaffolding wizard for each table CRUD Interface
Entity Framework VS wizards: Create models from database Create database from model Create model and database from db.model Create MVC controller and views from models
Entity Framework Very convenient Scaffolding wizard generates: Controller Views Time: minutes instead of hours Can add validation, customize view, etc.
LINQ Language Integrated Query Used by Entity Framework Alternative to SQL Select, sort, filter, etc. Used by Entity Framework Eliminates disconnect between database & code
MIS 324 -- Professor Sandvig 7/26/2019 LINQ - Intellisense We have data models Same as database LINQ examines models Provides Intellisense for writing queries
LINQ SQL Comparison Example: Sql: LINQ string sql = "select Id, Fname, Lname, street, city, state from students where id = @Id"; Execute query and map query results to model LINQ Student student = db.Students.Find(id);
LINQ - Intellisense See Student LINQ Controller handout
LINQ LINQ can query many collection types:
MIS 324 -- Professor Sandvig 7/26/2019 Drawbacks Microsoft propriety technology Not useful outside MS universe LINQ query syntax different than SQL Better in some ways Synchronization between database and entity objects Change database Rebuild entity objects
HW 1 Tutorial HW 1: Getting Started with Entity Framework 6 Database First using MVC 5 Steps: Create database with three tables: Students, courses, enrollments Create EF model with wizard Create controllers and view with wizard
Summary Entity Framework Very handy for CRUD operations VS wizards create: Models Controllers Views