Beginning ASP.NET 4.5 Databases i nwo t'loroon Sandeep Chanda Damien Foggon Apress-
Contents About the Author About the Technical Reviewer Acknowledgments Introduction xv xvii xix xxi Chapter 1: ASP.NET 4.5 Data Sources 1 The New Data Access Paradigms 2 Data Sources in ASP.NET 4.5 5 AD0.NET Data Sources 5 NoSQL 6 Web Services 6 WCF Data Services 6 Syndication Services 7 Azure Storage 7 HTML 5 Local Storage 7 Introduction to AD0.NET in.net Framework 4.5 8 What Is New inad0.net? 8 Entity Framework 5.0 8 SqICIient Data Provider 8 AD0.NET and LINQ 9 Data Providers 9 Asynchronous Data Access 10 Async Programming Model in.net Framework 4.5 11 vii
Public CONTENTS Developing Your First Example 13 Try It Out: First Data-Driven Page in ASP.NET 4.5 13 How It Works 25 Summary 25 Chapter 2: The Future of Relational Databases 27 The Evolution of Relational Databases 28 The Drawbacks of Relational Databases 28 How Microsoft SQL Server Is Overcoming the Limitations 28 Introducing Microsoft SQL Server 2012 29 What Is New? 29 Spatial Data 32 Spatial Data and SQL Server 32 Database Engine and T-SQL Enhancements 37 Support for Pagination 37 Sequence 38 Execute with Result Sets 40 FileTable 40 Data Visualization 40 Power View and Power Pivot 41 Self-Service Alerting 41 SQL Server 2012 Cloud 43 Private Cloud 44 SQL Azure - Cloud 44 Summary 47 Chapter 3: Introducing Non-Relational Databases 49 Understanding NoSQL Databases 49 Overview 50 Key Benefits 50 Popular NoSQL Technologies 50 Document-Oriented Databases 51 viii
Programming with MongoDB 52 How to Set Up MongoDB 52 Using the MongoDB Client 54 Programming MongoDB Using C# 55 How Does MongoDB Work? 59 Summary 59 &fchapter 4: Accessing Data Using ADO.NET 61 ADO.NET Architecture 61 Reading and Modifying a Database Using ADO.NET Data Providers 62 DataSetvs. DataReader 63 Connecting to a Database 64 Commands for CRUD Operations 66 Manipulating Data with Stored Procedures 66 Asynchronous Data Streams 68 Provider Factories 68 Multiple Active Result Sets 70 DataSets, DataAdapters, and DataSource Controls 71 Data Type Mappings 73 Precision with Floating-Point Numbers 74 Secure Data Access 74 Permissions for Accessing an SQL Database 74 Encrypting Web Configuration 75 Preventing Exploits Using Injection 76 Exception Handling 77 Secure String for Processing Sensitive Information in Memory 77 Microsoft SQL Server 2012 Extended Protection 77 Summary 77 Chapter 5: Introducing Language-Integrated Query (LINQ) 79 Writing Queries Using LINQ 79 LINQ Data Sources 80 Building Queries Using LINQ 83 ix
Query Operators 84 Query Operator Classification 85 Query Operator Execution 85 LINQ forado.net Data Access 85 LINQ to SQL 86 Lazy Loading 90 LINQ to DataSet 91 LINQ to Entities 92 LINQ and Parallelism 92 AsParallel Extension 92 Querying WCF Data Services 93 Summary 98 Chapter 6: ADO.NET Entity Data Model 99 EDM Architecture 99 Conceptual Schema 100 Storage Schema 102 Mapping Schema 103 EDM Generator Tool 104 AD0.NET Metadata 107 EDM Types and Relationships 107 DataSpace Enumerator 108 Metadata Workspace 108 Canonical and Store Functions 112 Introduction to Entity SQL 112 Summary 113 Chapter 7: Designing Data Access Applications Using Entity Framework 115 Entity Framework Architecture 116 Generating Entity Models 116 Entity Associations 121 Query EDM with LINQ to Entities 121 X
Code Generation in Entity Framework Using Text Templates 123 DbContext Generator and POCO Classes 125 POCO Proxy 128 Eager Loading Entities 129 Complex Types 129 Complex Types and Stored Procedures 131 Enumeration Types 134 Entity Client Data Provider 134 Unit Testing Your Data Access Layer 134 Summary 136 Chapter 8: Entity Framework Data Access Paradigms 137 Model-First Approach 138 Code-First Approach 148 Database Initializer 150 Custom Mapping with Fluent API 152 Code-First Migrations 153 Reverse Engineering Code First from Database 153 Validation with Data Annotations 154 Summary 156 Chapter 9: Consuming Data Using WCF Data Services 157 Overview of REST Semantics 158 Introduction to WCF Data Services 158 The Open Data Protocol 158 Exposing Data as Service 161 WCF Data Service Providers 166 Exposing ASP.NET MVC WebAPI Services as OData Feeds 167 Consuming Data Services in a Client Application 171 Consuming OData Feeds using jquery Ajax 171 Consuming OData Feeds Using WCF Data Services Client Library 173 Summary 175 xi
Chapter 10: Data Binding in ASP.NET 4.5 177 Data Source Controls 177 LinqDataSource 178 EntityDataSource 180 Data-Bound Controls 187 DataList 188 DetailsView 189 Query Extender 191 Strongly Typed Data Controls 191 ItemType Attribute 191 HTML-Encoded Expression 192 Model Binding 192 Value Providers 193 Data-Binding inasp.net MVC 4 194 Razor View Engine 194 Unobtrusive JavaScript 197 Asynchronous Display with Ajax 199 Summary 200 Chapter 11: Building Extensible Data-Driven Web Applications Using Dynamic Data 201 Dynamic Data Layers 201 Dynamic Data Scaffolding 202 Customizing the Scaffolding Templates 207 Template Behaviors 211 How Dynamic Data Works 211 MetaModel 211 Routing 212 Dynamic Data in Existing Controls 213 Summary 213 xii
Chapter 12: Best Practices and Techniques in Building Data-Driven Websites 215 Data Access Layer Patterns 215 Isolating the Data Layer and Entity Models 216 Repository Pattern 218 Unit of Work 223 Techniques to Deal with Issues 228 Concurrency 228 Transactions 229 Debugging Issues 229 Summary 234 Chapter 13: Application Lifecycle Management 235 Architecting the Enterprise 236 Designing New Applications 236 Analyzing Existing Application Codebase 238 Enhance Your Productivity 239 Better Access to Files and Code 239 Accessing Data 240 Suspend and Resume 241 Code Reviews 242 Analyze and Profile Your Application 244 Testing and Debugging Your ASP.NET Application 244 Unit Testing 245 Historical Debugging with IntelliTrace 245 Build and Deployment 245 Team Build Definition Team Build Definition Triggers Team Build Process 246 246 247 Deploy Using Team Build 247 Deploying SQL Server Databases 248 Summary 249