170 likes | 188 Views
Learn about Entity Framework, a powerful ORM tool for code-less CRUD web interfaces, as well as LINQ, a language integrated query for efficient data retrieval and manipulation. Discover how to generate models, controllers, and views, and synchronize your database with entity objects.
E N D
Entity Framework& LINQ (Language Integrated Query) MIS 324 Professor Sandvig
Outline • Entity Framework • Model generation • Scaffolding • Controllers & views • LINQ • Language integrated query
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 • Generates model of database tables • Example: • 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 • Createmodel 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 • Alternative to SQL • Select, sort, filter, etc. • Used by Entity Framework • Eliminates disconnect between database & code
LINQ - Intellisense • We have data models • Same as database • LINQ examines models • Provides Intellisense for writing queries
LINQ SQL Comparison • Example: • Sql: 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:
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