Browse by Tags

Tagged Content List
  • Blog Post: Tip 53 – How to debug EF POCO mapping issues

    If you are trying to use POCO classes in EF 4.0, it is relatively easy to run into problems with the mapping from your model to your CLR classes. If you get any nasty problems here, the best way to get to the bottom of things is to try to explicitly load the metadata for your POCO types. For example...
  • 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 41 – How to execute T-SQL directly against the database

    Sometimes you’ll find you need to issue a query or command that the Entity Framework can’t support. In fact this problem is common to most ORMs, which is why so many of them have a backdoor to the database. The Entity Framework has a backdoor too… .NET 3.5 SP1 In .NET 3.5 SP1 you can get to the underlying...
  • Blog Post: Tip 40 – How to materialize presentation models via L2E

    Problem: Imagine that you have these entities public class Product { public int ID { get; set; } public string Name { get; set; } public virtual Category Category { get; set; } } public class Category { public int ID { get; set; } public string Name { get; set; } public virtual List<Product> Products...
  • Blog Post: Tip 39 – How to set overlapping Relationships – EF 4.0 only

    Scenario: In EF 4 we have FK relationships , available for the first time in .NET 4.0 Beta 2, so it is now possible to have a model something like this: public class Division { public int DivisionID {get;set} // Primary Key public string Name {get;set;} public virtual List<Lawyer> Lawyers {get;set;...
  • 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 35 – How to write OfTypeOnly<TEntity>()

    If you write a LINQ to Entities query like this: var results = from c in ctx.Vehicles.OfType<Car>() select c; It will bring back, Cars and any type that derives from Car, like say SportCar or SUV. If you just want Cars and you don’t want derived types like SportCars or SUVs in LINQ to...
  • 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: Tip 29 – How to avoid LazyLoad or Load() reader issues

    If you have code that looks like this: var results = from c in ctx.Customers where c.SalesPerson.EmailAddress == “…” select c; foreach(var customer in results) { Console.WriteLine(customer.Name); if (IsInteresting(customer)) { customer.Orders.Load(); foreach(var order in customer.Orders) { Console...
  • Blog Post: Tip 28 - How to implement an Eager Loading strategy

    Background: Over the last 2 years lots of people have complained about the way Eager loading works in the Entity Framework, or rather the way you ask the Entity Framework to eagerly load. Here is how you do it: var results = from b in ctx.Blogs.Include(“Posts”) where b.Owner == “Alex” select b; This...
  • 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: 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...
  • 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 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 you have any questions our...
  • Blog Post: 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...
  • Blog Post: 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...
  • 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: 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...
  • 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 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 12 - How to choose an Inheritance Strategy

    What strategies does the Entity Framework support? The Entity Framework supports 3 primary inheritance strategies: Table Per Hierarchy (TPH): In TPH, all data for a type hierarchy is stored in one table, and there is a discriminator column that is used to establish the type of a particular row (i.e....
  • Blog Post: Tip 11 – How to avoid Relationship Span

    Background and Motivation: In my last post on EF Jargon I introduced the concept of Relationship Span. If you remember Relationship Span is basically responsible for compensating for the lack of Foreign Key properties* in your Entity. Relationship Span for an Entity, lets say StaffMember, insures...
  • 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...
Page 1 of 3 (64 items) 123