Programming Entity Framework: Code First
Julia Lerman
Rowan Miller
Copyright 2011 Julia Lerman, Rowan Miller
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of OReilly Media, Inc. Programming Entity Framework: Code First , the image of a common bittern, and related trade dress are trademarks of OReilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
O'Reilly Media
Preface
Microsofts principal data access technology, ADO.NET Entity Framework, has had two major releases as part of the .NET Framework. NET 3.5 brought us the first version of Entity Framework, which is covered in the first edition of Programming Entity Framework . In 2010, Microsoft .NET 4 was released; it contained the next version of Entity Framework, referred to as Entity Framework 4. The completely revised second edition of Programming Entity Framework was dedicated to teaching readers how to use this version of Entity Framework in Visual Studio 2010.
When .NET 4 was released, the Entity Framework team was already hard at work on a new addition, called Code First, to provide an alternative to building the Entity Data Model that is core to Entity Framework. Rather than using a visual designer, Code First allows you to create the model from your existing classes.
This book is dedicated to teaching readers how to use Code First to build and configure a model based on the classes in your business domain. While Code First can do much of the job of inferring a model from your classes, there is quite a lot that you can do to affect the model that Code First creates.
In this book, you will learn what Code First does by default (aka convention) and how to perform further configuration to affect how it understands your properties, classes, relationships, and the database schema they map towhether you use Code First to help create a database or you want to use it with an existing database. With this knowledge, you can reap the benefits of the Entity Framework while leveraging existing classes or those classes you might be building for a new software project.
Audience
This book is designed for .NET developers who have experience with Visual Studio and database management basics. Prior experience with Entity Framework is beneficial but not required. The code samples in this book are written in C#, with some of these samples also expressed in Visual Basic. There are a number of online tools that you can use to convert snippets of C# into Visual Basic.
Contents of This Book
This book contains eight chapters.
This chapter provides a high level, end-to-end overview of Code First. Youll find sample code, but there are no walkthroughs in this first chapter. The chapter winds up with a discussion of what you wont find in Code First, so that you can approach the technology with the correct expectations.
In this chapter, you will get to jump right in to the code. The chapter gives you a chance to work hands-on with Code First (or just read along if you prefer) as you work with a simple class to see some of the basic default behavior and perform some simple configurations using the two mechanisms for configuring: Data Annotations and the Fluent API. Youll see how Code First is able to automatically create a database for you using default behavior. Through the next four chapters youll lean on this database creation default, and then in , youll learn how to work with existing databases and exert more control over the database.
This is the first of three chapters that dive deeply into Code First convention and configuration. Youll learn about the presumptions the Code First convention makes about the attributes of properties (e.g., the length of strings) and how that gets interpreted into the conceptual model and the database. Youll also learn how to perform configuration using Data Annotations and Fluent API to control the outcome of the model and database.
In this chapter, youll focus on relationships between your classes and how those work out in the model that Code First infers and in the database. Code First convention is able to infer the most common scenarios when classes have relationships between them. Well look closely at the nuances in your classes that will drive Code Firsts assumptions and then how to ensure that Code First knows exactly what you want it to do, again by configuring with Data Annotations or the Fluent API.
This chapter focuses on how classes map to the database. This information will be especially important when you are mapping your classes to an existing database. Simple mappings, such as specifying table names or column names and types, can make a huge difference. Youll learn about the default mappings to the database when you have inheritance hierarchies defined between your classes and how to drive Table Per Hierarchy, Table Per Type and even Table Per Concrete Type mappings to the database. Youll also learn how to map a single entity to multiple tables or, conversely, multiple entities to a single table.
This chapter is where you finally get to stray from the default database creation behavior. Youll learn how to control Code Firsts determination of the database name and location, whether you do this through connection strings or some lower-level code in the Code First API. Youll also find some additional tricks for controlling connections and more.
In this chapter, youll dig farther into Code First to see how to perform some advanced techniques. Youll see how to prevent Code First from worrying about keeping the database in sync with your model when you want to take over control of that task. Youll also learn about the default model caching and how to override it to solve problems like targeting multiple database providers in the same application instance. Other advanced topics are addressed as well.
This book was written about the features of Code First based on the Entity Framework 4.2 release. At the time of this writing, a number of Community Technical Previews demonstrate some of the features that Code First will gain in upcoming releases. This chapter shares available information about these future releases.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.