Pro Entity Framework 4.0 (eBook)

(Autor)

eBook Download: PDF
2010 | 1st ed.
VIII, 280 Seiten
Apress (Verlag)
978-1-4302-0647-7 (ISBN)

Lese- und Medienproben

Pro Entity Framework 4.0 - Scott Klein
Systemvoraussetzungen
49,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

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.
Previously, SQL developers have been able to almost entirely ignore the SQLCLR and treat it as a peripheral technology-almost an extension to the main product. With the advent of LINQ and the Entity Framework, this is no longer the case, and the SQLCLR is moving to the center stage. It's a powerful product but, for many, it is an entirely new way of working with data. Pro Entity Framework 4.0 will help readers understand the implications of the Entity Framework and how it can be used to change their development practices and make their databases more powerful and flexible. Since many readers will be encountering this subject for the first time, the book will create an inclusive environment in which the concepts of .NET development are approached from the perspective of the Relational T-SQL developer to foster a sense of shared-ownership in keeping with Microsoft's latest best practices. Comprehensive coverage of the new greater and more challenging integration with .NET 3.5 Written from a real world perspective and examines the features offered by LINQ and the Entity Framework that will help solve problems experienced by professional developers Contains insight, interpretation, analysis, and evangelism instead of just plain fact

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.

Contents at a Glance 5
Table of Contents 6
About the Author 12
About the Technical Reviewer 13
Acknowledgments 14
CHAPTER 1 Introducing the ADO.NET 4.0 Entity Framework 15
The Need for an Entity Framework 15
This Has Been Tried Before 17
So, What Is the Entity Framework? 18
Database vs. Model 19
Database-Driven 19
Model-Driven 20
Working with Entities 21
Entity Framework 4.0 Features 23
POCO Support 24
Model-First Support 24
Related Object–Deferred Loading 24
LINQ-to-Entities Function Support 24
Plurality Naming 24
Complex Types 25
Customized Object-Layer Code Generation 25
Model Browser Improvements 25
Back-End Support 26
CHAPTER 2 The Entity Data Model 27
Creating an EDM 27
Taking a Database-First Approach 28
Making Generated Object Names Plural or Singular 33
Taking a Model-First Approach 36
Generating a Schema and Database 41
Managing Table Inheritance 45
Taking a Code-Only Approach 46
CHAPTER 3 The Entity Data Model Inside and Out 48
The Designer Window and the EDM 48
The Designer Window 48
Model Browser Window 50
Mapping Details Window 51
Entities 51
Scalar Properties 53
Complex Types 53
Complex Types Defined 54
Creating a Complex Type 54
Foreign Keys and Relationships (Associations) 58
Navigation Properties 59
Mapping Details 60
Lifting the EDM Hood 61
The EDM Model Parts 63
The SSDL Section 63
EntityType Element 65
Association Element 65
The CSDL Section 66
EntityType Element 67
Associations 68
The CS (MSL) Section 69
EDM-Generated Classes 71
CHAPTER 4 Querying the EDM 75
Querying with the Entity Framework 75
Syntax Options 75
Query-Expression Syntax 75
Context 79
Method-Based Syntax 80
Querying Options 81
LINQ to Entities 81
Entity SQL 86
The EntityClient 88
EntityConnection 89
EntityCommand 90
Immediate vs. Deferred Query Execution 90
Deferred Execution 91
Immediate Execution 92
CHAPTER 5 Working with Entities 94
The ObjectContext 94
ObjectStateEntry 94
Tracking and Saving Changes 95
Updating Entities 96
Adding Entities 98
Relational Inserts 100
Deleting Entities 102
CHAPTER 6 Stored Procedures and the EDM 104
Stored Procedures in the EDM 104
The Model Browser 108
What Is an EF Function? 109
Mapping Functions 110
Functions (Stored Procedures) in Action 113
Insert 113
Update 115
Delete 116
Select 116
Using Functions in Queries 117
CHAPTER 7 Relationships and Associations 120
Overview 120
Relationships in General 120
Relationships in EF 3.5 121
EF 4.0 Relationships 124
Creating a WinForms Project 124
Defining Referential Constraints 127
Adding an Association 127
Looking at XML Differences 128
Understanding Approaches to Foreign Keys in EF 4.0 130
Using FK Associations in Code 131
Adding Dependent Objects 131
Manually Setting the Foreign Key Property 132
Setting the Foreign Key Automatically 133
Building the Sample Project 134
Summary 137
CHAPTER 8 T4 Code Generation 138
T4 Template Overview 138
Adding a Template Using Visual Studio 2008 138
Installing a T4 Editor 139
Writing Some T4 Code 140
Scoping Your Code 143
Example 1: Running the Project 144
Example 2: Returning Your Computer’s Processes 145
Example 3: Listing Your SQL Databases 146
T4 Templates and the Entity Framework 148
T4 Customization Example 153
CHAPTER 9 Model-First Development 155
Model-First Design 155
Creating a Conceptual Model 155
Creating Entities in the Empty Model 158
Creating Associations and Navigation Properties 160
Saving the Model 161
Verifying Compilation 162
Creating the Mappings and Database 162
Database Generation Rules 169
Tables 169
Association 170
Handling of Complex Types 171
DB Generation Script Customization 173
CHAPTER 10 Code-Only Development 177
Getting Started with Code-Only 177
Creating the Data Project 178
Adding the User-Interface Project 180
Adding References 180
Adding Context and Connections 182
Coding the User Interface 183
Creating Configuration Classes 183
Testing the Code-Only Model 185
Building the Project 186
Loading Some Rows 187
Connecting the DataGridView Control 188
Running the Application 189
Overcoming Restrictions in EF 3.5 190
Additional POCO Information 190
Complex-Type Support 191
Lazy Loading 192
Change Tracking 192
Finishing the Example 193
CHAPTER 11 N-tier Development with WCF Data Services 197
Building the WCF Data Service 197
Testing the WCF Data Service 204
Consuming the WCF Data Service 211
Adding the Service Reference 212
Utilizing the Service 216
CHAPTER 12 Performance Tuning and Exception Handling 219
Updating the Model 219
Checking the Model 223
Stored Procedure Mapping 225
Building an Entity Framework Project 226
Exception Handling 229
Try/Catch Blocks 229
The Using Statement 231
Exception Class 231
Connection Exceptions 233
Query Exceptions 235
EntitySQL Exceptions 236
CHAPTER 13 Data Binding with the Entity Framework 238
Windows Forms Data Binding 238
Creating a Project Directory 238
Creating a New Form 238
Creating Data Sources 241
Adding Code to the Form 245
Adding a Grid Control 247
Getting the Grid to Display Some Data 248
Helping the Grid to Navigate the Relation 250
Testing Your Final Grid Implementation 250
Implementing Add, Delete, and Save Functionality 251
Implementing Add Functionality 251
Implementing Delete Functionality 253
Implementing Save Functionality 253
WPF Data Binding 254
Creating a Project 254
Adding Some Code 254
Running the Project 255
Displaying Related Detail 257
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."

Erscheint lt. Verlag 29.4.2010
Zusatzinfo VIII, 280 p.
Verlagsort Berkeley
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Betriebssysteme / Server
Informatik Datenbanken SQL Server
Mathematik / Informatik Informatik Software Entwicklung
Schlagworte ADO • data model • Microsoft SQL Server • .NET • SQL • SQL Server 2005 • SQL Server 2008
ISBN-10 1-4302-0647-0 / 1430206470
ISBN-13 978-1-4302-0647-7 / 9781430206477
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 18,5 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
A Practical Guide to Analyzing Performance in SQL Server and Azure …

von Thomas LaRock; Enrico van de Laar

eBook Download (2023)
Apress (Verlag)
62,99