Browse by Tags

Tagged Content List
  • Blog Post: 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>...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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...
  • Blog Post: 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 you build...
  • Blog Post: 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...
  • Blog Post: 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 interesting...
  • Blog Post: 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...
  • Blog Post: 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 tips to make it...
  • Blog Post: 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...
  • Blog Post: Tip 13 - How to Attach an Entity the easy way

    The Problem: In some of earlier tips we talked about using Attach to load things into the ObjectContext in the unchanged state without wearing the cost of doing a query. Attach is the weapon of choice if performance is your goal. Unfortunately our APIs aren't tailored for the 99% case, which is where...
  • Blog Post: Tips and Tricks

    Hopefully if you're reading this you've noticed that I've started a series of Tips recently. The tips will mostly apply to Entity Framework or Data Services. Seeing as I expect to have a lots of tips, it probably makes sense to have some sort of index. That is what this is post is, as I add...
  • Blog Post: Tip 1 - How to sort Relationships in Entity Framework

    Question: One of the questions you often see in the Entity Framework Forums is around sorting related items. For example imagine if you want to query customers, and return only those that have accounts more than 30 days in arrears, and at the same time retrieve their orders. And you need those...
  • Blog Post: DefiningQuery versus QueryView

    Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 ...
  • Blog Post: MEST - What is it and how does it work?

    MEST stands for M ultiple E ntity S ets per T ype. It is one of the features of the Entity Framework that is typically missing from ORM stacks. The idea is that you can store the same EntityType in multiple EntitySets. For the purposes of this discussion you can think of EntitySets as analogous to Tables...
  • Blog Post: Lazy-loading and the Entity Framework

    Most of you probably know that the Entity Framework architects made a very conscious decision to make all trips to the database explicit. The result is that this: Person mother = person.Mother; will return null, even if the person has a Mother, unless you either eager load (using Include) or explicitly...
  • Blog Post: Fantasy Soccer - part 3 - Unit of Measure

    Today I spent a bit of time working on the data model for my fantasy soccer application. I don't want to talk about the actual model today though, because I'm not really ready. What I want to do is talk about a problem that occurred to me, namely Unit Of Measure. Any real sport fan knows you...
  • Blog Post: Intellisense : What properties are required?

    Yesterday I was talking to a Simon, another super smart dev on the EF team, and he raised a big concern, one that I am sure you can all relate too: We've all seen something like this: And thought: "What is the bare minimum I can do here to save a Whatever ?" This "What properties are required...
  • Blog Post: Colin Meek talks about his EF Extensions

    Colin is a super smart colleague of mine, who works as a developer on the Entity Framework. So it is great to see him start a blog . His first post is super cool, because he discusses a set of extensions he put together that get around some of mapping limitations of the Entity Framework. He...
  • Blog Post: Maybe there is more

    Michel Perfetti , has taken my little Maybe thingie and gone a lot further , by using expression tree re-writting he has made it possible to express this: string code = licensePlate.Maybe(lp => lp.Car) .Maybe(c => c.Owner) .Maybe(o => o.Address) .Maybe(a => a.PostCode); which is...
  • Blog Post: Maybe I'm just a little slow...

    But I only just realized that extension methods are cool for avoiding NullReferenceExceptions . We all know that if you have something like this: LicensePlate licensePlate = null ; Car car = licensePlate.Car; It will throw a NullReferenceException . However if you have an extension...
  • Blog Post: Associations with Payloads - closing the loop

    I'm assuming you've read part 1 and part 2 , if not why not? Just kidding, they will make this post a little easier to understand though. I'm going to show you how to create a read-only association with a Payload. Okay so imagine you have this model: And you want to have be able to do this...
  • Blog Post: All the power at your disposal

    I invite you to take a look at this forum post from 'Benys' It illustrates the power of the Entity Framework's mapping capabilities + partial classes. The short answer to a lot of "Does the EF support ... ?" type questions is often no. Often however the longer answer, is a little easier to digest. If...
Page 1 of 2 (35 items) 12