Book Image

Entity Framework 4.1: Expert's Cookbook

By : Devlin Liles , Tim Rayburn
Book Image

Entity Framework 4.1: Expert's Cookbook

By: Devlin Liles , Tim Rayburn

Overview of this book

<p>Entity Framework 4.1 allows us to dive into the world of data access without having to write SQL statements. With the power to shape data access by your object model comes questions and this book holds the answers.</p> <p>Entity Framework 4.1: Expert's Cookbook holds many examples to help guide you through tough technical decisions and avoid technical landmines. The book will guide you from just using Entity Framework to enhancing the data access wizard.</p> <p>This book starts with examples that require some familiarity of object relational mappers, and then moves on to more advanced tasks. You will be guided through complex mapping scenarios, query definition, reusability, integration with other technologies, and architectural management. The approach is step-by-step and test driven so that it is focused as much as possible on solving problems and getting the most out of the time spent working through the book.</p> <p>Entity Framework 4.1: Expert's Cookbook is a must have for any .NET developer who uses Entity Framework, and wants better, cleaner, and more maintainable code.</p>
Table of Contents (15 chapters)
Entity Framework 4.1: Expert's Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating one-to-one maps


This recipe will allow us to map the direct relationships between two objects into the foreign keys that will hold this relationship in the database.

Getting ready

We will be using NuGet Package Manager to install the Entity Framework 4.1 assemblies.

The package installer can be found at http://nuget.org/.

We will also be using a database for connecting to and updating data.

Open the Improving One-To-One References solution in the included source code examples.

How to do it...

Let us get connected to the database using the following steps:

  1. We start by adding a new unit test named MappingTest to the Test project. We make a test that connects to the database and retrieves an object. This will test the configuration and ensure that the model matches the database schema. Use the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BusinessLogic;
    using DataAccess;
    using DataAccess.Database;
    using Microsoft.VisualStudio.TestTools...