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…

2

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 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…

4

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;…

5

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;}    public…

1

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 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…

4

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

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.WriteLine(“\t” + order.Value);         }     } } This code…

1

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…

20