Programming Entity Framework: Code First

Book description

Take advantage of the Code First data modeling approach in ADO.NET Entity Framework, and learn how to build and configure a model based on existing classes in your business domain. With this concise book, you’ll work hands-on with examples to learn how Code First can create an in-memory model and database by default, and how you can exert more control over the model through further configuration.

Code First provides an alternative to the database first and model first approaches to the Entity Data Model. Learn the benefits of defining your model with code, whether you’re working with an existing database or building one from scratch. If you work with Visual Studio and understand database management basics, this book is for you.

  • Learn exactly what Code First does—and does not—enable you to do
  • Understand how property attributes, relationships, and database mappings are inferred from your classes by Code First
  • Use Data Annotations and the Fluent API to configure the Code First data model
  • Perform advanced techniques, such as controlling the database schema and overriding the default model caching

This book is a continuation of author Julia Lerman’s Programming Entity Framework, widely recognized as the leading book on the topic.

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Audience
    2. Contents of This Book
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
    7. Acknowledgments
  2. 1. Welcome to Code First
    1. Modeling with EF Before Code First
    2. Inception of Code First
    3. Getting Code First to Developers in Between .NET Releases
    4. Writing the Code…First
    5. Managing Objects with DbContext
    6. Using the Data Layer and Domain Classes
    7. Getting from Classes to a Database
    8. Working with Configuration
      1. Configuring with Data Annotations
      2. Configuring with the Fluent API
    9. Creating or Pointing to a Database
    10. What Code First Does Not Support
      1. Choosing Code First
    11. Learning from This Book
  3. 2. Your First Look at Code First
    1. Introducing EF to the Domain Classes
    2. Putting the Pieces Together in an Example
      1. Convention for Table, Schema, and Column Names
      2. Convention for Keys
      3. Convention for String Properties
      4. Convention for Byte Array
      5. Convention for Booleans
      6. Convention for One-to-Many Relationships
    3. Overriding Convention with Configurations
      1. Configuring with Data Annotations
    4. Understanding How Model Changes Impact Database Initialization
      1. Data Annotations and Validation-Aware UIs
    5. Configuring Code First with the Fluent API
      1. Organizing Fluent Configurations
    6. Summary
  4. 3. Using Conventions and Configurations for Property Attributes
    1. Working with Property Attributes in Code First
      1. Length
      2. Data Type
      3. Nullability and the Required Configuration
    2. Mapping Keys
      1. Code First Convention Response to Unconventional Key Properties
      2. Configuring the Key with Data Annotations
      3. Using HasKey to Configure a Key Property in the Fluent API
    3. Configuring Database-Generated Properties
      1. Configuring Database-Generated Options with Data Annotations
      2. Configuring Database-Generated Options with the Fluent API
    4. Configuring TimeStamp/RowVersion Fields for Optimistic Concurrency
      1. Code First Convention and TimeStamp fields
      2. Using Data Annotations to Configure TimeStamp
      3. Configuring TimeStamp/RowVersion with Fluent API
    5. Configuring Non-Timestamp Fields for Concurrency
      1. Configuring for Optimistic Concurrency with Data Annotations
      2. Configuring for Optimistic Concurrency with Fluent API
    6. Mapping to Non-Unicode Database Types
    7. Affecting the Precision and Scale of Decimals
      1. Convention for Precision and Scale
      2. Data Annotations for Precision and Scale
      3. Fluent Configuration for Precision and Scale
    8. Working with Complex Types in Code First
      1. Defining Complex Types by Convention
      2. Configuring Unconventional Complex Types
        1. Specifying complex types with Data Annotations
        2. Specifying complex types with the Fluent API
      3. Working with More Complicated Complex Types
    9. Configuring Properties of Complex Types
      1. Configuring Complex Types with Data Annotations
      2. Configuring Complex Type Properties with the Fluent API
    10. Summary
  5. 4. Using Convention and Configuration for Relationships
    1. Working with Multiplicity
      1. Configuring Multiplicity with Data Annotations
      2. Configuring Multiplicity with the Fluent API
    2. Working with Foreign Keys
      1. Specifying Unconventionally Named Foreign Keys
        1. Fixing foreign key with Data Annotations
        2. Fixing foreign key with the Fluent API
    3. Working with Inverse Navigation Properties
    4. Working with Cascade Delete
      1. Turning On or Off Client-Side Cascade Delete with Fluent Configurations
      2. Setting Cascade Delete Off in Scenarios That Are Not Supported by the Database
    5. Exploring Many-to-Many Relationships
    6. Working with Relationships that Have Unidirectional Navigation
    7. Working with One-to-One Relationships
      1. Configuring One-to-One Relationships When Both Ends Are Required
        1. Ensuring that the sample code honors the required Photo
        2. Configuring one-to-one with the Fluent API
    8. Summary
  6. 5. Using Conventions and Configurations for Database Mappings
    1. Mapping Class Name to Database Table and Schema Name
      1. Configuring Table and Schema Name with Data Annotations
      2. Configuring Table and Schema Name with the Fluent API
    2. Mapping Property Names to Database Columns
      1. Modifying the Default Column Name with Data Annotations
      2. Modifying the Default Column Name with the Fluent API
      3. Affecting Column Names for Complex Types
    3. Allowing Multiple Entities to Map to a Single Table: aka Table Splitting
      1. Mapping to a Common Table using Data Annotations
      2. Splitting a Table Using the Fluent API
    4. Mapping a Single Entity Across Multiple Tables
    5. Controlling Which Types Get Mapped to the Database
      1. Preventing Types from Being Included in the Model
        1. Using Data Annotations to ignore types
        2. Using Fluent Configuration to ignore types
    6. Understanding Property Mapping and Accessibility
      1. Scalar Property Mapping
      2. Accessibility of Properties, Getters, and Setters
    7. Preventing Properties from Being Included in the Model
      1. Data Annotations for Ignoring a Property
      2. Fluent Configuration for Ignoring a Property
    8. Mapping Inheritance Hierarchies
      1. Working with Code First’s Default Inheritance: Table Per Hierarchy (TPH)
      2. Customizing the TPH Discriminator Field with the Fluent API
      3. Configuring Table Per Type (TPT) Hierarchy
      4. Configuring for Table Per Concrete Type (TPC) Inheritance
      5. Avoiding Mapping Exceptions with TPC
    9. Working with Abstract Base Classes
    10. Mapping Relationships
      1. Controlling Foreign Keys Included in Your Class
      2. Controlling Foreign Keys That Are Created by Code First
        1. Controlling generated foreign keys with entity splitting
      3. Controlling Many-to-Many Join Tables
    11. Summary
  7. 6. Controlling Database Location, Creation Process, and Seed Data
    1. Controlling the Database Location
      1. Controlling Database Location with a Configuration File
      2. Controlling Database Name with DbContext Constructor
      3. Controlling Connection String Name with DbContext Constructor
      4. Reusing Database Connections
      5. Controlling Database Location with Connection Factories
        1. Working with SqlConnectionFactory
        2. Working with SqlCeConnectionFactory
        3. Writing a custom connection factory
    2. Working with Database Initialization
      1. Controlling When Database Initialization Occurs
      2. Switching Off Database Initialization Completely
      3. Database Initializers Included in Entity Framework
      4. Creating a Custom Database Initializer
      5. Setting Database Initializers from a Configuration File
    3. Using Database Initializers to Seed Data
    4. Using Database Initialization to Further Affect Database Schema
    5. Summary
  8. 7. Advanced Concepts
    1. Mapping to Nontable Database Objects
      1. Mapping to Updatable Views
      2. Using Views to Populate Objects
      3. Using Views to Populate Nonmodel Objects
      4. Working with Stored Procedures
    2. Removing Conventions
    3. Taking Control of Model Caching
      1. Understanding Model Caching
      2. Overriding Default Model Caching
    4. Working with the EdmMetadata Table
      1. Coding Against EdmMetadata
      2. Preventing Code First from Creating and Seeking EdmMetadata
    5. Using Code First with ObjectContext
    6. Summary
  9. 8. What’s Coming Next for Code First
    1. Code First Migrations
    2. Entity Framework Power Tools
      1. Reverse Engineer Code First
      2. Viewing a Code First Model
        1. View Entity Data Model (Read-only)
        2. View Entity Data Model XML
        3. View Entity Data Model DDL SQL
      3. Optimize Entity Data Model
  10. About the Authors
  11. Copyright

Product information

  • Title: Programming Entity Framework: Code First
  • Author(s): Julia Lerman, Rowan Miller
  • Release date: November 2011
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449312947