Tip 26 – How to avoid database queries using Stub Entities

What are Stub Entities? A stub entity is a partially populated entity that stands in for the real thing. For example this: Category c = new Category {ID = 5}; is a stub entity. It has only the ID populated, which indicates this is a stub for Category 5. When are Stub Entities Useful? Stub…

21

Wrapping Providers

For well over a year now I’ve been talking about how it is possible with EF to write a provider that sits between the EF and the native database provider. There are lots of reasons why you might do this. Here are some examples, you could write a: Tracing Provider Auditing Provider Caching Provider Query…

1

Tip 25 – How to Get Entities by key the easy way

Sometimes rather than writing this: var customer = ctx.Customers.First(c => c.ID == 5); You would rather write something like this: var customer = ctx.Customers.GetCustomerById(5); In .NET 4.0 this would be trivial to do by modifying the T4 templates that do code gen. You would simply produce an extension method for each EntityType, something like this:…

5

Tip 24 – How to get the ObjectContext from an Entity

Customers often ask how to get from an Entity back to the ObjectContext . Now generally we don’t recommend trying this. But sometimes you really need a way to get to the ObjectContext. For example if you are in method and all you have is the Entity, and you need the ObjectContext perhaps to do…

7

Tip 21 – How to use the Single() operator – EF 4.0 only

This is 21st post in my ongoing series of Entity Framework Tips, and the first that is specific to EF 4.0. Entity Framework 4.0 Beta 1 As you’ve probably heard VS 2010 Beta 1 is now available to subscribers which means some of you can get your hands on EF 4.0 Beta 1 too. If…

3

Tip 20 – How to deal with Fixed Length Keys

This is 20th post in my ongoing series of Entity Framework Tips. Fixed Length Field Padding: If you have an fixed length column in the database, for example something like NCHAR(10) when you do an insert, padding happens automatically. So for example if you insert ‘12345’ you get 5 spaces automatically appended, to create a…

6

Tip 19 – How to use Optimistic Concurrency with the Entity Framework

This is the 19th post in my ongoing series of Entity Framework Tips. Background: If you have a table with a timestamp column, and you reverse engineer an Entity from that table, you will end up with a Binary property in your entity (in my example called Version). If you look at the properties window…

9

Tip 17 – How to do one step updates with AttachAsModified(..)

Background: In Tip 13 – How to Attach the easy way I showed you how to ‘establish’ the EntitySet for a particular CLR Type so you could Attach it. But in all the tips so far the same basic pattern is used: Attach the original version of the Entity Modify it SaveChanges I’ve given you…

2

Interesting reads

Over the weekend in between run throughs that I did prepping for a webcast, I did some catch-up blog reading. Lots of interesting stuff. Recording and sharing here for posterity: Entity Framework Simon Segal has a interesting post on Entity Framework Repository testing. I found this particularly timely because of my upcoming TechEd talk about…

3

Tip 15 – How to avoid loading unnecessary Properties

UPDATE: Made a couple of important corrections re-which Original Values are required. Problem: Imagine if you query blog posts: var myPosts = from post in ctx.Posts                orderby post.Created descending               select post; Just so you can output the post titles etc. foreach(var post in myPosts) {      Console.WriteLine(“{0} on {1}”, post.Title, post.Created); } Well…

4