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 47 – How fix-up can make it hard to change relationships

Problem: Take this code: Category oldCategory = ctx.Categories     .Include(“Products”)     .First(c => c.Name == “Drink”); Category newCategory = new Category {Name = “Beverage”}; foreach(Product product in oldCategory.Products) {     newCategory.Products.Add(product); } Ignore for a second that in this example the solution is probably just to rename the oldCategory “Beverages” – Coming up with real…

2

Tip 46 – How to exclude a property using Code-Only

This time a real simple one prompted by this question on StackOverflow. Problem: If you tell the Entity Framework about this class using Code-Only, by default every property becomes part of Entity, and as a result stored in the database. Usually this is what you want. But not always, imagine this class: public class Person{…

1

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 33 – How cascade delete really works in EF

Imagine that in your database you have a cascade delete on an FK relationship. Something like this: Here the Delete Rule says that when a Category is deleted all the related Products should be deleted too. If you generate an EF model from this database you get a model that on the surface looks no…

6

Tip 32 – How to create a database from SSDL – EF 4 only

We recently released a CTP that extends EF 4 Beta 1 which included Code Only. You can read more about Code Only here, here and here. If you look at the walkthroughs for Code Only you will see code that looks something like this: // Create a builder and configure it var builder = new…

0

Tip 30 – How to use a custom database function

Imagine you have a database function like the DistanceBetween function in Nerd Dinner: CREATE FUNCTION [dbo].[DistanceBetween](   @Lat1 as real,   @Long1 as real,   @Lat2 as real,   @Long2 as real)RETURNS realASBEGIN…END And you want to use it with the Entity Framework. Declaring the Function The first step is to open your EDMX file in the XML editor…

11

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 produce an extension method for each EntityType, something like this:…

5