Welcome to MSDN Blogs Sign in | Join | Help

Browse by Tags

All Tags » Object Services   (RSS)
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. Read More...
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 Read More...
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 Read More...
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 Read More...
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, Read More...
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 Read More...
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 Read More...
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 Read More...
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, Read More...
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 Read More...
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 Read More...
Page view tracker