Tenny Hirani Entity Framework 4.0 Recipes Companion eBook Available Entity Framework 4.0 Recipes A Problem-Solution Approach 7.5 x 9.25 spine = 1.21875" 648 page count THE EXPERT’S VOICE ® IN .NET Larry Tenny and Zeeshan Hirani Ready-made solutions for putting Microsoft Entity Framework 4.0 to work in your own applications this print for content only—size & color not accurate CYAN MAGENTA YELLOW BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Larry Tenny Shelve in: Microsoft .NET User level: Intermediate–Advanced THE APRESS ROADMAP Pro Entity Framework 4.0 Pro LINQ: Language Integrated Query in C# 2010 Introducing .NET 4.0 with Visual Studio 2010 Beginning SQL Server 2008 for Developers Entity Framework Recipes Expert SQL Server 2008 Development www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2703-8 9 781430 227038 5 49 9 9 Entity Framework 4.0 Recipes: A Problem-Solution Approach Dear Reader, You want to start using Entity Framework 4.0, but you’re a little overwhelmed by all the new terms and concepts. Want to get started right away? You’ve come to the right place. Entity Framework is Microsoft’s core data access technology. It represents a completely new way to build data-driven applications. Here in this book you’ll find well over a hundred recipes that help you get started with Entity Framework and solve problems without having to wade through chapter after chapter of terminol- ogy and theory. Want to create your first model? We’ve got recipes for that. Want to model Table per Type inheritance? We’ve got recipes for that. Want to use Entity Framework with Windows Communication Foundation? We’ve got recipes for that! If you have a problem, we have a short, to-the-point recipe that will give you the solution and a clear explanation of how it works. This book assumes just a basic knowledge of databases and .NET development. You don’t need to be an expert developer or database administrator to use these recipes. Each recipe provides a clear statement of the problem it solves. That way, you can quickly scan for your problem and get right to the solution. We give you step-by-step directions and provide a complete working example in each recipe. The recipes are completely independent so you never have to flip around trying to tie together a complete concept. Entity Framework is Microsoft’s key data-enabling technology for now and for years to come. With Microsoft’s Entity Framework, Visual Studio 2010, .NET 4.0, and this book, you’re all set to develop some incredibly powerful applications using the latest technologies and practices on the planet. We hope you enjoy the book. Larry Tenny and Zeeshan Hirani Zeeshan Hirani Entity Framework 4.0 Recipes A Problem-Solution Approach Larry Tenny Zeeshan Hirani Entity Framework 4.0 Recipes: A Problem-Solution Approach Copyright © 2010 by Larry Tenny and Zeeshan Hirani All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2703-8 ISBN-13 (electronic): 978-1-4302-2704-5 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher and President: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewers: David Annesley-DeWinter, Brian Swan Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Nancy Sixsmith Compositor: Bytheway Publishing Services Indexer: Toma Mulligan Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. You can download the examples from the book’s catalog page: http://apress.com/book/view/1430227036. Look for the “Source Code” link underneath the cover image. You will need to answer questions pertaining to this book in order to successfully download the code. To the most important people in my life, my wife and kids. – Larry I would like to dedicate this book to my parents for encouraging me and supporting me always. Special thanks to my Dad for his great love and support in everything. – Zeeshan CONTENTS Contents at a Glance Contents at a Glance iv Contents v About the Authors xxx About the Technical Reviewers xxxi Acknowledgments xxxii Preface xxxiii Chapter 1: Getting Started With Entity Framework 1 Chapter 2: Entity Data Modeling Fundamentals 9 Chapter 3: Querying an Entity Data Model 63 Chapter 4: Using Entity Framework in ASP.NET 115 Chapter 5: Loading Entities and Navigation Properties 155 Chapter 6: Beyond the Basics with Modeling and Inheritance 189 Chapter 7: Working with Object Services 251 Chapter 8: Plain Old CLR Objects 271 Chapter 9: Using the Entity Framework in N-Tier Applications 311 Chapter 10: Stored Procedures 359 Chapter 11: Functions 393 Chapter 12: Customizing Entity Framework Objects 429 Chapter 13: Improving Performance 471 Chapter 14: Concurrency 509 Chapter 15: Advanced Modeling 529 Index 591 iv v Contents Contents at a Glance iv Contents v About the Authors xxx About the Technical Reviewers xxxi Acknowledgments xxxii Preface xxxiii Who This Book Is For xxxiii What’s in This Book xxxiv About the Recipes xxxv Stuff You Need to Get Started xxxvi Code Examples xxxvi The Database xxxvi Apress Website xxxvii Chapter 1: Getting Started With Entity Framework 1 A Brief Tour of the Entity Framework World 2 Models 2 Terminology 3 Code 4 Visual Studio 2010 4 Using Entity Framework 5 CONTENTS vi Chapter 2: Entity Data Modeling Fundamentals 9 2-1. Creating a Simple Model 9 Problem 9 Solution 9 How It Works 14 2-2. Creating a Model from an Existing Database 16 Problem 16 Solution 16 How It Works 19 2-3. Modeling a Many-to-Many Relationship with No Payload 22 Problem 22 Solution 22 How It Works 23 2-4. Modeling a Many-to-Many Relationship with a Payload 26 Problem 26 Solution 26 How It Works 27 2-5. Modeling a Self-Referencing Relationship 29 Problem 29 Solution 29 How It Works 30 2-6. Splitting an Entity Across Multiple Tables 33 Problem 33 Solution 33 How It Works 35 2-7. Splitting a Table Across Multiple Entities 37 Problem 37 Solution 37 CONTENTS vii How It Works 40 2-8. Modeling Table per Type Inheritance 42 Problem 42 Solution 42 How It Works 44 2-9. Using Conditions to Filter an ObjectSet 46 Problem 46 Solution 46 How It Works 48 2-10. Modeling Table per Hierarchy Inheritance 49 Problem 49 Solution 49 How It Works 52 2-11. Modeling Is-a and Has-a Relationships Between Two Entities 54 Problem 54 Solution 55 How It Works 56 2-12. Creating, Modifying, and Mapping Complex Types 57 Problem 57 Solution 58 How It Works 59 Chapter 3: Querying an Entity Data Model 63 3-1. Executing an SQL Statement 63 Problem 63 Solution 63 How It Works 65 3-2. Returning Objects from a SQL Statement 66 Problem 66 CONTENTS viii Solution 66 How It Works 67 3-3. Returning Objects from an Entity SQL Statement 68 Problem 68 Solution 68 How It Works 70 3-4. Specifying Fully Qualified Names in Entity SQL 71 Problem 71 Solution 72 How It Works 74 3-5. Finding a Master that Has Detail in a Master-Detail Relationship 74 Problem 74 Solution 74 How It Works 76 3-6. Setting Default Values in a Query 77 Problem 77 Solution 77 How It Works 79 3-7. Returning Multiple Result Sets From a Stored Procedure 80 Problem 80 Solution 80 How It Works 81 3-8. Comparing Against a List of Values 82 Problem 82 Solution 82 How It Works 84 3-9. Building and Executing a Query Against an ObjectSet<T> 85 Problem 85 [...]... and a brief synopsis of the recipes you’ll find in them: Chapter 1: Getting Started with Entity Framework We explain the motivation behind Entity Framework We also explain what the framework is and what it does for you Chapter 2: Entity Data Modeling Fundamentals This chapter covers the basics in modeling Here you’ll find out how to get started with modeling and with Entity Framework in general If you’re... chapter probably has the recipes you’re looking for Chapter 3: Querying an Entity Data Model We’ll show you how to query your model using both LINQ to Entities and Entity SQL Chapter 4: Using Entity Framework in ASP.NET Web applications are an important part of the development landscape, and Entity Framework is ideally suited for ASP.NET In this chapter we focus on using the EntityDataSource to interact... on Entity Framework Chapter 9: Using Entity Framework in n-Tier Applications The recipes in this chapter cover a wide range of topics using Entity Framework across the wire We cover POCO, self-tracking entities, serialization, and concurrency Chapter 10: Stored Procedures If you are developing or maintaining a real-world, data-centric application, you most likely work with stored procedures The recipes. .. contains well over 150 recipes that you can put to work right away Entity Framework is a large and complex topic Perhaps it’s too big for a monolithic reference book In this book, you will find direct and self-contained answers to just about any problem you’re facing in building your Entity Framework- powered applications Along the way, you’ll learn an enormous amount about Entity Framework xxxiii PREFACE... and not just rows and columns There is no question that Entity Framework is the future of data access for the Microsoft platform The first release in August of 2008 was widely considered a good first step Now, more than year later, this new release of Entity Framework (often called EF 4.0) as part of the newly released Visual Studio 2010 and NET 4.0 has matured into a full function data access technology... applications to others This makes our code much more maintainable Entity Framework is not just for developers Microsoft is aggressively positioning the modeling concepts in Entity Framework to serve as the conceptual domain for Reporting Services and Integration Services as well as other technologies that process, report on, and transform data Entity Framework is quickly becoming a core data access foundation... Loading Entities and Navigation Properties The recipes in this chapter cover just about every possibility for loading entities from the database Chapter 6: Beyond the Basics with Modeling and Inheritance Modeling is a key part of Entity Framework This is the second of three chapters with recipes specifically about modeling In this chapter, we included recipes that cover many of the more complicated,... content represented in this book We also would like to thank Noam Ben-Ami, Microsoft Program Manager, who answered many of our questions about Entity Framework Designer Noam provided incredibly important insight into some of the most interesting aspects of Entity Framework We would like to thank our technical editors, David Annesley-DeWinter and Brian Swan, for their careful and meticulous review of every... NET 4.0 release He has a PhD in Computer Science from Indiana University Zeeshan Hirani is a longtime NET and database developer He is a senior developer at a top Internet e-commerce site using Entity Framework, ASP.NET, Silverlight, and many other Microsoft technologies He has extensive experience with many ORM and database technologies, which provides him with a unique perspective on Microsoft’s Entity. .. applications, David moved to Washington and now works on the Entity Framework team at Microsoft, where he focuses on features for object services like the POCO templates and Code First In his spare time, David enjoys photography and rowing for the Sammamish Rowing Association in Redmond You can read more about his experiences with the Entity Framework and other NET-related topics on his blog at http://blogs.rev-net.com/ddewinter/ . ROADMAP Pro Entity Framework 4. 0 Pro LINQ: Language Integrated Query in C# 201 0 Introducing .NET 4. 0 with Visual Studio 201 0 Beginning SQL Server 200 8 for Developers Entity Framework Recipes Expert SQL. Server 200 8 Development www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $ 10 eBook version ISBN 978-1 -43 02 -2 703 -8 9 78 143 0 22 703 8 5 49 9 9 Entity Framework 4. 0 Recipes: A. Inheritance 200 Problem 200 Solution 200 How It Works 202 6-5. Modeling a Self-Referencing Relationship and Retrieving a Complete Hierarchy .2 04 Problem 2 04 Solution 2 04 How It Works 207 6-6. Mapping