Tip 42 – How to create a dynamic model using Code-Only

Background: When we give examples of how to use Code-Only we always start with a strongly typed Context derived from ObjectContext. This class is used to bootstrap the model. For example this (property bodies omitted for simplicity sake): public class MyContext : ObjectContext {     public ObjectSet<Category> Categories { get; }    public ObjectSet<Product> Products…

5

Tip 37 – How to do a Conditional Include

Problem Someone asked how to do a Conditional Include a couple of days ago on StackOverflow. They wanted to query for some entity (lets say Movies) and eager load some related items (lets say Reviews) but only if the reviews match some criteria (i.e. Review.Stars == 5). Unfortunately though this isn’t strictly supported by EF’s…

13

Tip 36 – How to Construct by Query

While writing my tips series and writing EF controllers for MVC I found that I regularly wanted to create and attach a stub entity. Unfortunately it isn’t quite that simple, you have to make sure the entity isn’t already attached first, otherwise you’ll see some nasty exceptions. To avoid these exceptions I often found myself…

6

Tip 34 – How to work with Updatable Views

UPDATE: thanks Zeeshan for pointing out that by default only non-nullable columns end up in the key for view backed entities. Imagine this situation, you have a view in your database, and it is updatable. Next you decide to use this view with the Entity Framework, so you go ahead and import it. The resulting…

16

Another C# trick: Fluents, Inheritance and Extension Methods

Here’s a scenario which Damien and I encountered recently. We needed a way of specifying Facets for properties on Entities, i.e. things like Nullable, MaxLength, Precision etc. And we needed a class hierarchy because different types of properties have different sets of available facets. The base class of the hierarchy is called PropertyConfiguration which defines…

12

A silly C# trick from the trenches

UPDATE: as Joe rightly points out in the comments (thanks Joe) when you try to pass a Func<T,V> to a method expecting Action<T> using lambda syntax it doesn’t fail. Somehow the compiler must realize there is no match for Func<T,V> and look for a match for with Action<T> as a parameter. I don’t know how…

7

Tip 27 – How to Implement BeforeSave Validation

It is common to want to validate that your entities are ‘valid’ before you save them to the database. A naive form of validation might be to try to ensure that your entities are ‘valid’ before you add them to the context, but this doesn’t help you if you pass through ‘invalid’ states, i.e. as…

4

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

Tip 23 – How to fake Enums in EF 4

As of right now Enums are not in EF4. Now we will be listening to your feedback about Beta1, and making some adjustments, so you never know, but at the moment it doesn’t look like they will be supported. Yesterday though I came up with a workaround that, while a bit of work, is pretty…

51

Tip 22 - How to make Include really Include

This is 22nd post in my ongoing series of Entity Framework Tips. If you want to do eager loading with the Entity Framework it is generally really easy, you simply write something like this: var results =         from post in ctx.Posts.Include(“Comments”)        where post.Author.EmailAddress == “alexj@microsoft.com”        select post; In this example for each post that matches you will get back…

22