1 / 36

ASP.NET MVC Working with Data

ASP.NET MVC Working with Data. Nikolay K ostov. Telerik Software Academy. Senior Software Developer and Trainer. http://nikolay.it. Table of Contents. ADO.NET Entity Framework and LINQ Code-first approach Scaffolding Model Binders Data Validation Kendo UI Grids.

katelyn
Download Presentation

ASP.NET MVC Working with Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET MVCWorking with Data Nikolay Kostov Telerik Software Academy Senior Software Developer and Trainer http://nikolay.it

  2. Table of Contents • ADO.NET Entity Framework and LINQ • Code-first approach • Scaffolding • Model Binders • Data Validation • Kendo UI Grids

  3. ADO.NET Entity Framework Object Relation Persistence Framework

  4. Overview of ADO.NET EF • Entity Framework (EF) is a standard ORM framework, part of .NET • Provides a run-time infrastructure for managing SQL-based database data as .NET objects • The relational database schema is mapped to an object model (classes and associations) • Visual Studio has built-in tools for generating Entity Framework SQL data mappings • Data mappings consist of C# classes and XML • A standard data manipulation API is provided

  5. Entity Framework Features • Entity Framework (EF) standard features: • Maps tables, views, stored procedures and functions as .NET objects • Provides LINQ-based data queries • Executed as SQLSELECTs on the database server • CRUD operations – Create/Read/Update/Delete • Create compiled queries – for executing the same parameterized query multiple times • Creating or deleting the database schema

  6. Entity Framework Lifecycle • When the application starts • EF translates into SQL the language-integrated queries in the object model • Sends them to the database for later execution

  7. Entity Framework Lifecycle (2) • When the database returns the results • Entity Framework translates the database rows back to .NET objects • The database server is transparent, hidden behind the API • LINQ is executed over IQueryable<T> • At compile time a query expression tree is emitted • At runtime SQL is generated and executed

  8. EF Components • The ObjectContextclass • ObjectContextholds the database connection and the entity classes • Provides LINQ-based data access • Implements identity tracking, change tracking, and API for CRUD operations • Entity classes • Each database table is typically mapped to a single entity class (C# class)

  9. EF Components (2) • Associations • An association is a primary key / foreign key based relationship between two entity classes • Allows navigation from one entity to another, e.g. Student.Courses • Concurrency control • EntityFramework uses optimistic concurrency control (no locking by default) • Provides automatic concurrency conflict detection and means for conflicts resolution

  10. LINQ • Comprehension Query Syntax • Extension Method Syntax

  11. Entity Framework SQL Server (compact) Schema First (database) MySQL Entity Framework (+ LINQ) Model First (model designer) Azure (cloud) Code First (C# classes) Oracle

  12. What isMicrosoft SQL Server? • MS SQL Server is a Relational Database Management System(RDBMS) from Microsoft • The main language supported in SQL Server isTransact SQL (T-SQL), an extension of SQL • Powerful, trustworthy, easy-to-use DB server • The most recent version isSQL Server 2012 • Works only on Windows systems • A free distribution exists (SQL Server Express) • http://www.microsoft.com/express/database/

  13. Entity Framework Models Design First Code First Model First Create .edmx model in designer Generate DB from .edmx Classes auto-generate from .edmx Code First Define classes & mapping in code Database auto-created at runtime New Database Code First Define classes & mapping in code Database First Reverse engineer .edmx model Classes auto-generate from .edmx Existing Database

  14. Code-First Approach

  15. Code First Approach • Convention over configuration • Database naming • Primary Key • Relationships (navigation properties) • Data Annotations • Tells EF how to map the object model to the database model • Place annotations directly against the property in your class • System.ComponentModel.DataAnnotations

  16. Common Data Annotations • Key – Defines a Primary Key • Column – Defines DB column name • Table – Defines table name for a class • Required – Defines a Required DB field • NotMapped – Property not in DB mapping • MinLength() – Min length for a property • MaximumLength() – Max length for property • Range() – Defines a valid value range

  17. Database Migrations • Package Manager Console • Enable-Migrations (-ContextTypeName) • This creates a Migration folder in project • Creates Configuration.csfile • AutomaticMigrationsEnabled = true; • Seed method to populate some initial data • Creates __MigrationHistory system table in DB • Update-Database (-Verbose) • Pushes the migration changes to the DB • –script – create a SQL script of the changes

  18. Custom Mapping • When the database doesn’t match conventions …

  19. Demo: Create Entities and Context Please work, please work, please work…

  20. Scaffolding

  21. Controller and View Templates • TODO: Editor templates and view templates

  22. Model Binders

  23. Model Binders • To make east of handling HTTP post request • Help the populating the parameters in action methods DefaultModelBinder HTTP POST /Review/Create Rating=7&Body=Great!

  24. Custom Model Binder

  25. Data Validation

  26. Validation with Annotations • Attributes defined in System.ComponentModel.DataAnnotations • Covers common validation patterns • Required • StringLength • Regex • Range

  27. Data Validation Attributes

  28. Custom Validation • Custom attributes • Inherit ValidationAttribute

  29. Validating Model – Controller • ModelState.IsValid – will give us information about the data validation success • ModelState.AddModelError – custom error

  30. Validating Model – View • @Html.ValidationSummary– output errors • @Html.ValidationMessageFor(…) – outputs validation message for specified property Text box with integrated client-side validation jQuery validation library required for unobtrusive JavaScript validation

  31. Display / Edit Annotations

  32. Kendo UI Grids

  33. TODO: Kendo Grids

  34. Summary • TODO: Summary

  35. ASP.NET MVCWorking with Data http://schoolacademy.telerik.com

  36. Free Trainings @ Telerik Academy • “C# Programming @ Telerik Academy • csharpfundamentals.telerik.com • Telerik Software Academy • academy.telerik.com • Telerik Academy @ Facebook • facebook.com/TelerikAcademy • Telerik Software Academy Forums • forums.academy.telerik.com

More Related