Entity Framework 4.0 improvements

I’ve been a huge fan of LINQ to SQL. But EF 3.5 (first release) didn’t quite do it for me. I didn’t like the lack of support for foreign keys, no Single() LINQ support,

No Single support

An example of lack of foreign keys:

using(NorthwindEntities northwindEntities = new NorthwindEntities())
{
    Product p = new Product()
                {
                    Category = northwindEntities.Categories.Where(c => c.CategoryID == 1).First,
                    ProductName = "French fries",
                    UnitPrice = 20,
                    Discontinued = false,
                    QuantityPerUnit = "1 kg bag",
                    Supplier = northwindEntities.Suppliers.Where(s => s.SupplierID == 1).First

                };

    northwindEntities.AddToProducts(p);
    northwindEntities.SaveChanges();
}

As you can see you need to fetch  the category and the Supplier seperatly. You can’t just do CategoryID = 1

But when we launch the wizard for EF 4.0 we get this:

Include foreign key columns in the model

Which results in this:

using(NorthwindEntities northwindEntities = new NorthwindEntities())
{
    Product p = new Product()
                {
                    CategoryID = 1,
                    ProductName = "French fries",
                    UnitPrice = 20,
                    Discontinued = false,
                    QuantityPerUnit = "1 kg bag",
                    SupplierID = 1

                };

    northwindEntities.AddToProducts(p);
    northwindEntities.SaveChanges();
}

And there is Single support!

using(NorthwindEntities ne = new NorthwindEntities())
{
    var result = ne.Suppliers.Where(sup => sup.SupplierID == 1).Single();
}

So it just got a little more mature.

I’m hoping to be able to test it (and use it!) more in the future and new projects :)

Hope it helps

-Kristof

3 thoughts on “Entity Framework 4.0 improvements

  1. Quote: “As you can see you need to fetch the category and the Supplier seperatly. You can’t just do CategoryID = 1″

    Sure you can — with a few lines of extra code.

    This is perfectly possible in the EF pre-4.0 by extending your Product Entity with a custom class. In the extended entity you add a CategoryID property which gets and sets the ID value on the CategoryEntityReference. It will then update the relationship accordingly whenever you assign the CategoryID.

  2. Quick addendum: This will not update the Category Entity reference in the Product Entity. To get this you will still have to fetch the Product Entity with an included reference to the Category. Using this workaround is (only) handy for inserting and or updating your products.

  3. The problem with that is, and you say it yourself, you have to extend the class. This slows down the development. In RAD you don’t want that. You just want off the shelf products and glue them together.

    That is where EF pre-4.0 is bad at. It takes too much time to find the quirks and glue everything together.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>