Browse by Tags

Tagged Content List
  • 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 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 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...
  • Blog Post: 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...
  • Blog Post: Tip 18 – How to decide on a lifetime for your ObjectContext

    One of the most common questions we get is how long should an ObjectContext should live. Options often cited include one per: Function Form Thread Application Plenty of people are asking these types of questions, case in point here is a question on Stackflow . I am sure there are many more buried away...
  • 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 16 – How to mimic .NET 4.0’s ObjectSet<T> today

    Background: In order to be an EF power user today you really need to be familiar with EntitySets . For example you need to understand EntitySets in order to use AttachTo(…) or create EntityKeys . In most cases there is only one possible EntitySet for each object / clr type. It is this idea that...
  • Blog Post: Tip 14 - How to cache Entity Framework Reference Data

    Scenario: In order to make applications perform it makes a lot of sense to cache commonly used reference data. Good examples of reference data include things like States, Countries, Departments etc. Generally you want to have this data readily at hand, so you can populate dropdown boxes etc. A good example...
  • 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: Tip 6 - How and when to use eager loading

    When should you use eager loading? Usually in your application you know what you are going to "do" with an entity once you have retrieved it. For example if you retrieve an order so you can re-print it for a customer, you know that the re-print would be incomplete without information about the items...
Page 1 of 1 (11 items)