E-Book, Englisch, 280 Seiten
Klein Pro Entity Framework 4.0
1. ed
ISBN: 978-1-4302-0647-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
E-Book, Englisch, 280 Seiten
ISBN: 978-1-4302-0647-7
Verlag: Apress
Format: PDF
Kopierschutz: 1 - PDF Watermark
The introduction of SQL Server 2005 began the process which has seen .NET become an integral part of the SQL Server database technology. SQL Server 2008 is the next evolutionary step in that story. Alongside the more traditional database enhancements in the new release, there is an increasing focus upon a greater and more challenging integration with .NET 3.5. This book shows how to take advantage of this tight integration and how developers and DBAs can manipulate data more easily than ever before.
Scott Klein is a Microsoft SQL Server 'Most Valuable Professional' and independent consultant specializing in SQL Server performance and business intelligence. Scott is the author of several books, including Professional SQL Server 2005 XML, Professional LINQ, and Pro ADO.NET 4.0 Entity Framework. He was a contributing author to Pro SQL Server 2008 Relational Database Design and Implementation, as well as to the Microsoft SQL Server 2008 Bible. Scott has written many articles for the SQL Server Standard magazine. He holds the MCDBA, MCSD, and MCSE certifications. He is heavily involved in the south Florida community, running two SQL Server user groups and the South Florida SQL Saturday events. Scott also speaks frequently at user groups across south Florida. Scott has nearly 20 years working with SQL Server and consulting in small to enterprise-size environments.
Autoren/Hrsg.
Weitere Infos & Material
1;Contents at a Glance;5
2;Table of Contents;6
3;About the Author;12
4;About the Technical Reviewer;13
5;Acknowledgments;14
6;CHAPTER 1 Introducing the ADO.NET 4.0 Entity Framework;15
6.1;The Need for an Entity Framework;15
6.1.1;This Has Been Tried Before;17
6.1.2;So, What Is the Entity Framework?;18
6.2;Database vs. Model;19
6.2.1;Database-Driven;19
6.2.2;Model-Driven;20
6.3;Working with Entities;21
6.4;Entity Framework 4.0 Features;23
6.4.1;POCO Support;24
6.4.2;Model-First Support;24
6.4.3;Related Object–Deferred Loading;24
6.4.4;LINQ-to-Entities Function Support;24
6.4.5;Plurality Naming;24
6.4.6;Complex Types;25
6.4.7;Customized Object-Layer Code Generation;25
6.4.8;Model Browser Improvements;25
6.5;Back-End Support;26
7;CHAPTER 2 The Entity Data Model;27
7.1;Creating an EDM;27
7.2;Taking a Database-First Approach;28
7.3;Making Generated Object Names Plural or Singular;33
7.4;Taking a Model-First Approach;36
7.5;Generating a Schema and Database;41
7.6;Managing Table Inheritance;45
7.7;Taking a Code-Only Approach;46
8;CHAPTER 3 The Entity Data Model Inside and Out;48
8.1;The Designer Window and the EDM;48
8.1.1;The Designer Window;48
8.1.2;Model Browser Window;50
8.1.3;Mapping Details Window;51
8.2;Entities;51
8.2.1;Scalar Properties;53
8.2.2;Complex Types;53
8.2.2.1;Complex Types Defined;54
8.2.2.2;Creating a Complex Type;54
8.2.3;Foreign Keys and Relationships (Associations);58
8.2.4;Navigation Properties;59
8.3;Mapping Details;60
8.4;Lifting the EDM Hood;61
8.5;The EDM Model Parts;63
8.5.1;The SSDL Section;63
8.5.1.1;EntityType Element;65
8.5.1.2;Association Element;65
8.5.2;The CSDL Section;66
8.5.2.1;EntityType Element;67
8.5.2.2;Associations;68
8.5.3;The CS (MSL) Section;69
8.6;EDM-Generated Classes;71
9;CHAPTER 4 Querying the EDM;75
9.1;Querying with the Entity Framework;75
9.2;Syntax Options;75
9.2.1;Query-Expression Syntax;75
9.2.2;Context;79
9.2.3;Method-Based Syntax;80
9.3;Querying Options;81
9.3.1;LINQ to Entities;81
9.3.2;Entity SQL;86
9.4;The EntityClient;88
9.4.1;EntityConnection;89
9.4.2;EntityCommand;90
9.5;Immediate vs. Deferred Query Execution;90
9.5.1;Deferred Execution;91
9.5.2;Immediate Execution;92
10;CHAPTER 5 Working with Entities;94
10.1;The ObjectContext;94
10.1.1;ObjectStateEntry;94
10.1.2;Tracking and Saving Changes;95
10.2;Updating Entities;96
10.3;Adding Entities;98
10.4;Relational Inserts;100
10.5;Deleting Entities;102
11;CHAPTER 6 Stored Procedures and the EDM;104
11.1;Stored Procedures in the EDM;104
11.2;The Model Browser;108
11.3;What Is an EF Function?;109
11.4;Mapping Functions;110
11.5;Functions (Stored Procedures) in Action;113
11.5.1;Insert;113
11.5.2;Update;115
11.5.3;Delete;116
11.5.4;Select;116
11.6;Using Functions in Queries;117
12;CHAPTER 7 Relationships and Associations;120
12.1;Overview;120
12.1.1;Relationships in General;120
12.1.2;Relationships in EF 3.5;121
12.2;EF 4.0 Relationships;124
12.2.1;Creating a WinForms Project;124
12.2.2;Defining Referential Constraints;127
12.2.3;Adding an Association;127
12.2.4;Looking at XML Differences;128
12.2.5;Understanding Approaches to Foreign Keys in EF 4.0;130
12.3;Using FK Associations in Code;131
12.3.1;Adding Dependent Objects;131
12.3.2;Manually Setting the Foreign Key Property;132
12.3.3;Setting the Foreign Key Automatically;133
12.4;Building the Sample Project;134
12.5;Summary;137
13;CHAPTER 8 T4 Code Generation;138
13.1;T4 Template Overview;138
13.1.1;Adding a Template Using Visual Studio 2008;138
13.1.2;Installing a T4 Editor;139
13.1.3;Writing Some T4 Code;140
13.2;Scoping Your Code;143
13.2.1;Example 1: Running the Project;144
13.2.2;Example 2: Returning Your Computer’s Processes;145
13.2.3;Example 3: Listing Your SQL Databases;146
13.3;T4 Templates and the Entity Framework;148
13.4;T4 Customization Example;153
14;CHAPTER 9 Model-First Development;155
14.1;Model-First Design;155
14.1.1;Creating a Conceptual Model;155
14.1.2;Creating Entities in the Empty Model;158
14.1.3;Creating Associations and Navigation Properties;160
14.1.4;Saving the Model;161
14.1.5;Verifying Compilation;162
14.1.6;Creating the Mappings and Database;162
14.2;Database Generation Rules;169
14.2.1;Tables;169
14.2.2;Association;170
14.3;Handling of Complex Types;171
14.4;DB Generation Script Customization;173
15;CHAPTER 10 Code-Only Development;177
15.1;Getting Started with Code-Only;177
15.1.1;Creating the Data Project;178
15.1.2;Adding the User-Interface Project;180
15.1.3;Adding References;180
15.1.4;Adding Context and Connections;182
15.1.5;Coding the User Interface;183
15.1.6;Creating Configuration Classes;183
15.1.7;Testing the Code-Only Model;185
15.1.8;Building the Project;186
15.1.8.1;Loading Some Rows;187
15.1.8.2;Connecting the DataGridView Control;188
15.1.8.3;Running the Application;189
15.1.9;Overcoming Restrictions in EF 3.5;190
15.2;Additional POCO Information;190
15.2.1;Complex-Type Support;191
15.2.2;Lazy Loading;192
15.2.3;Change Tracking;192
15.3;Finishing the Example;193
16;CHAPTER 11 N-tier Development with WCF Data Services;197
16.1;Building the WCF Data Service;197
16.2;Testing the WCF Data Service;204
16.3;Consuming the WCF Data Service;211
16.3.1;Adding the Service Reference;212
16.3.2;Utilizing the Service;216
17;CHAPTER 12 Performance Tuning and Exception Handling;219
17.1;Updating the Model;219
17.2;Checking the Model;223
17.3;Stored Procedure Mapping;225
17.4;Building an Entity Framework Project;226
17.5;Exception Handling;229
17.5.1;Try/Catch Blocks;229
17.5.2;The Using Statement;231
17.5.3;Exception Class;231
17.5.4;Connection Exceptions;233
17.5.5;Query Exceptions;235
17.5.6;EntitySQL Exceptions;236
18;CHAPTER 13 Data Binding with the Entity Framework;238
18.1;Windows Forms Data Binding;238
18.1.1;Creating a Project Directory;238
18.1.2;Creating a New Form;238
18.1.3;Creating Data Sources;241
18.1.4;Adding Code to the Form;245
18.1.5;Adding a Grid Control;247
18.1.5.1;Getting the Grid to Display Some Data;248
18.1.5.2;Helping the Grid to Navigate the Relation;250
18.1.5.3;Testing Your Final Grid Implementation;250
18.1.6;Implementing Add, Delete, and Save Functionality;251
18.1.6.1;Implementing Add Functionality;251
18.1.6.2;Implementing Delete Functionality;253
18.1.6.3;Implementing Save Functionality;253
18.2;WPF Data Binding;254
18.2.1;Creating a Project;254
18.2.2;Adding Some Code;254
18.2.3;Running the Project;255
18.2.4;Displaying Related Detail;257
19;Index;261
"Model-First Development (p. 145-146)
In the last chapter we focused on how to use text templates to customize the generation of the EDM. T4 has been incorporated in many facets in EF 4.0, and this chapter will build on that. One of the things requested by EF developers was the ability to generate a database based on the EDM. In the previous version of EF you could build an EDM starting with an empty model, but you couldn’t do anything with it after that. More specifically, you could not build or create your database based on your EDM.
EF 4.0 fixes that problem, and not only lets you build your database based on your EDM, but also lets you customize the DDL that is generated. This chapter will focus on two aspects of model-first design, the first being the ability to build an EDM and to then create the database based on your EDM. The second part of the chapter will utilize the information you gained in the previous chapter by using T4 templates and Windows Workflow to customize the output of the DDL.
Model-First Design
One of the most glaring and almost agonizing exclusions from the first release of the Entity Framework was a complete model-first solution. With EF V1, you could create a model from scratch, but you could not really do much with mapping and database creation. Anyone who spent any time on the MSDN Entity Framework forums knows that creating the model first was one of the most requested pieces of functionality. Microsoft listened, and, with Version 4.0 of the Entity Framework, they delivered. With version 4.0 of the Entity Framework, you now have a true “model-first” solution.
Once you have your conceptual model created, you can now derive the storage model, mappings, and database from your conceptual model, all from a single menu item on the Designer context menu. From this menu you can generate a database schema directly from your model as well as the appropriate mappings. Microsoft also provides the ability to customize the database creation process through T4 templates, giving developers much-needed flexibility and control over how the mappings and the schema are generated. I’m getting goose bumps.
This section will walk you through the entire process, from creating the conceptual model to the creation of the database and mappings. Creating a Conceptual Model Let’s begin our model-first design by creating a somewhat simple model. Create a new Class Library project and name the project ModelFirst. We are not going to add any user interface components, so we don’t need to create a Windows Forms application for this example.
Our model, and subsequent database, is going to track motocross teams, their riders, and the class in which each rider races. In the sport of motocross, a rider can actually race in multiple classes, but we don’t want anything that complicated for this example. For the sake of this example, a rider will race a single class. Unlike other sports, in the sport of motocross a rider rarely changes, or “gets traded” to, another team during the year. So we won’t worry about a rider changing teams either. In this example we will create an EDM and then use a new feature to generate a database based on our model. Figure 9-1 shows the New Project creation screen—nothing new here. Pick the project type, enter the project name and click OK."




