Programming Entity Framework:
Code First
Julia Lerman and Rowan Miller
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
Programming Entity Framework: Code First
by Julia Lerman and Rowan Miller
Copyright © 2012 Julia Lerman, Rowan Miller. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Meghan Blanchette
Production Editor: Teresa Elsey
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Revision History for the First Edition:
2011-11-18 First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449312947 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Programming Entity Framework: Code First, the image of a common bittern, and
related trade dress are trademarks of O’Reilly 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 O’Reilly 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 con-
tained herein.
ISBN: 978-1-449-31294-7
[LSI]
1321545608
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1.
Welcome to Code First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Modeling with EF Before Code First 1
Inception of Code First 2
Getting Code First to Developers in Between .NET Releases 3
Writing the Code…First 4
Managing Objects with DbContext 6
Using the Data Layer and Domain Classes 7
Getting from Classes to a Database 8
Working with Configuration 10
Configuring with Data Annotations 10
Configuring with the Fluent API 11
Creating or Pointing to a Database 12
What Code First Does Not Support 12
Choosing Code First 13
Learning from This Book 15
2. Your First Look at Code First . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Introducing EF to the Domain Classes 18
Putting the Pieces Together in an Example 18
Convention for Table, Schema, and Column Names 22
Convention for Keys 23
Convention for String Properties 23
Convention for Byte Array 24
Convention for Booleans 24
Convention for One-to-Many Relationships 24
Overriding Convention with Configurations 25
Configuring with Data Annotations 25
Understanding How Model Changes Impact Database Initialization 27
Data Annotations and Validation-Aware UIs 30
iii
- 1
- 2
前往页