Entity Framework 6 Recipes

2nd Edition

By Brian Driscoll , Nitin Gupta , Robert Vettor , Zeeshan Hirani , Larry Tenny

Entity Framework 6 Recipes Cover Image

Entity Framework 6 Recipes teaches the core concepts of Entity Framework through a broad range of clear and concise solutions to everyday data access tasks.

Full Description

  • ISBN13: 978-1-4302-5788-2
  • 548 Pages
  • User Level: Intermediate to Advanced
  • Publication Date: November 3, 2013
  • Available eBook Formats: EPUB, MOBI, PDF
  • Print Book Price: $49.99
  • eBook Price: $34.99
Buy eBook Buy Print Book Add to Wishlist

Related Titles

Full Description

Entity Framework 6 Recipes provides an exhaustive collection of ready-to-use code solutions for Entity Framework, Microsoft's model-centric, data-access platform for the .NET Framework and ASP.NET development. With this book, you will learn the core concepts of Entity Framework through a broad range of clear and concise solutions to everyday data access tasks. Armed with this experience, you will be ready to dive deep into Entity Framework, experiment with new approaches, and develop ways to solve even the most difficult data access challenges. If you are a developer who likes to learn by example, then this is the right book for you.

  • Gives ready-to-use, real-world recipes to help you with everyday tasks
  • Places strong focus on DbContext and the Code First approach
  • Covers new features such as Asynch Query and Save, Codebased Configuration, Connection Resiliency, Dependency Resolution, and much more

What you’ll learn

  • Implement basic data access design patterns using Entity Framework.
  • Seamlessly model your solutions across both code and data.
  • Provide data access to Windows 8 and Metro applications.
  • Integrate with WCF Data Services
  • Improve data access performance.
  • Simplify and reduce your code through data binding.

Who this book is for

Entity Framework 6 Recipes is for anyone learning Microsoft’s Entity Framework—Microsoft’s primary data access platform in the .NET Framework. If you have ever struggled to learn a new technology, programming model, or way of doing something, you know how helpful simple and real-world examples can be. For the beginning developer, this book provides concrete examples for common data access tasks. For developers having experience with previous Microsoft data access platforms, this book provides a task-by-task mapping between previous approaches and the patterns used in Entity Framework.

Table of Contents

Table of Contents

  1. Getting Started with Entity Framework
  2. Entity Data Modeling Fundamentals
  3. Querying an Entity Data Model
  4. Using Entity Framework in ASP.NET
  5. Loading Entities and Navigation Properties
  6. Beyond the Basics with Modeling and Inheritance
  7. Working with Object Services
  8. Plain Old CLR Objects
  9. Using the Entity Framework in N-Tier Applications
  10. Stored Procedures
  11. Functions
  12. Customizing Entity Framework Objects
  13. Improving Performance
  14. Concurrency
Source Code/Downloads

Downloads are available to accompany this book.

Your operating system can likely extract zipped downloads automatically, but you may require software such as WinZip for PC, or StuffIt on a Mac.

Errata

Please Login to submit errata.

On page 24:

artist1.Albums.Add(album);
artist2.Albums.Add(album);
context.Albums.Add(album);

does not work. It only creates an album record without artists linked. One possible solution is making the reverse linking. For example:

album.Artists.Add(artist1);
album.Artists.Add(artist2);
context.Albums.Add(album);


On page 25:

Output of Recipe3 is
Artists and their albums...
Alan Jackson
Drive
Live at Texas Stadium

Albums and their artists...
Drive
Alan Jackson
Live at Texas Stadium
Alan Jackson
Honkytonk University


I have to change the code to add artists to album before adding a new album to the context (not the other way round).
// add an album for two artists
var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };
var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };
var album = new Album { AlbumName = "Honkytonk University" };
album.Artists.Add(artist1);
album.Artists.Add(artist2);
context.Albums.Add(album);
context.SaveChanges();

On page 26:
using (var context = new EF6RecipesContext())
{
var order = new Order { OrderId = 1,
OrderDate = new DateTime(2010, 1, 18) };
var item = new Item { SKU = 1729, Description = "Backpack",
Price = 29.97M };
var oi = new OrderItem { Order = order, Item = item, Count = 1 };

item = new Item { SKU = 2929, Description = "Water Filter",
Price = 13.97M };
oi = new OrderItem { Order = order, Item = item, Count = 3 };
item = new Item { SKU = 1847, Description = "Camp Stove",
Price = 43.99M };
oi = new OrderItem { Order = order, Item = item, Count = 1 };
context.Orders.Add(order);
context.SaveChanges();
}

//should add this in order to make the source code to work--->order.OrderItems.Add(oi);

On page 29:

I have to replace

public List<PictureCategory> Subcategories { get; set; }

by

public virtual List<PictureCategory> Subcategories { get; set; }

in order to list the children

On page 30:
public PictureContext() : base("name=EF6CodeFirstRecipesContext")
{
}

must be:

public EF6RecipesContext() : base("name=EF6CodeFirstRecipesContext")
{
}

On page 31:
In order to achive the expected result, is necessary to change:

context.PictureCategories.Add(paris);

for

context.PictureCategories.Add(vacation);

On page 52:
Figures are mixed up. Caption for 2.23 needs to be 2.24 and vice versa.

On page 74:
The following sentence does not work because you're not using T-SQL.

var employees = context.Database.SqlQuery<Employee>(esql);

You are running an Entity SQL statement which uses ObjectContext instead of DbContext.

var employees = ((IObjectContextAdapter)context).ObjectContext.CreateQuery<Employee>(esql);

On page 161-162:

You need to disable explicit loading because otherwise the example doesn't show his purpose

// explicitly disable lazy loading
context.Configuration.LazyLoadingEnabled = false;

The output should be the following:

Executive Suites for Grand Seasons Hotel with reservations

Executive Suite 14 is $299,95 per night
Current reservations are:
18/01/2010 thru 28/01/2010 (Bill Meyers)
12/03/2010 thru 14/03/2010 (Roberta Jones)
Press <enter> to continue...

On page 172:

The output of the code is wrong. in Listing 5-31.
Line 4 should say:

"Dr. Steven Mills was explicitly loaded"

not

"Dr. Steven Mills was lazy loaded"


On page 176:

Listing 5-36.
AddObject no longer exists.

the last section should read
context.Categories.Add(cat1);
context.Categories.Add(cat2);