Freigeben über


Associations with Payloads

Those of you who know what I am talking about will probably also know that we have been saying publicly that the Entity Framework doesn't support them.

Now for those who don't know what an association with a payload is, imagine something like:

Order <- (1-*) -> OrderLine <- (*-1) -> Product

An OrderLine is an Entity in it's own right, it has an identity, an FK point to the Order and an FK pointing to the Product, and extra properties like a Quantity for example.

Conceptually however you can also think of the OrderLine as being a Many-2-Many association, that associates Orders with Products, and additional has a payload: the Quantity.

Order <- (*-*) -> Product

Now the benefit of being able to treat the OrderLine as an association as well as an Entity is that you can replace 2 step navigations (Order.OrderLines.Product or Product.OrderLines.Order) with one step navigations (Order.Products or Product.Orders).

So for example I could write something like this:

var products = from o in ctx.Orders
from p in o.Products
where o.OrderNo = 454
select p;

When the Entity Framework tools encounter a pure Many-2-Many join table that has only 2 columns, both of which are Foreign Keys, it creates an Association instead of an Entity. But if the table has more columns, like OrderLine which has a PK and a Quantity, then an entity is created instead.

So the Entity Framework doesn't support Associations with Payloads natively.

It turns out however that with a little jigger pokery in the CSDL, MSL and SSDL it does support them, sort of.

In my next post I'll show you how, and explain the limitations.

Comments

  • Anonymous
    February 23, 2008
    PingBack from http://www.biosensorab.org/2008/02/23/associations-with-payloads/
  • Anonymous
    February 24, 2008
    Based on a previous discussion, you know I'll be waiting for that next post! :-)Julie
  • Anonymous
    February 24, 2008
    The suspense is deafening!--rjPingback from http://oakleafblog.blogspot.com/2008/02/linq-and-entity-framework-posts-for_19.html
  • Anonymous
    February 24, 2008
    Hi Alex.That's right but there is a restriction. OrderLine columns, else than OrderId and ProdcutId which are keys, must allow null (of course). Else, you can't do it. I thought to use a default value but it I don't think it's possible on an association.So the only way to do it is to change OrderLine ssdl, say that all columns (except keys) allow null and to use a ssdl function (stored procedure or a SQL function defined in ssdl part). With this, you can use a default value or use more interesting calculation. For example, imagine that you have a column quantity which doesn't allow null in your DB, you can count how many you have and add one on INSERT function.
  • Anonymous
    February 25, 2008
    J'en avais parlé aux techdays mais je vais profiter du post d' Alex pour en reparler et pour aller un
  • Anonymous
    February 25, 2008
    I try to do it with partial extension of entity types. I blog about it. My post is in French but what do you think of the code? Can I get the context without using reflection ?
  • Anonymous
    February 25, 2008
    Okay so the 'cat is out of the bag' you CAN do associations with Payloads... Sort of . Now for an explanation
  • Anonymous
    February 25, 2008
    I'm assuming you've read part 1 and part 2 , if not why not? Just kidding, they will make this post a
  • Anonymous
    February 25, 2008
    I&#39;m assuming you&#39;ve read part 1 and part 2 , if not why not? Just kidding, they will make this
  • Anonymous
    February 28, 2008
    If you have a many to many relationship in your database, the Entity Data Model wizard will flatten the