Welcome to MSDN Blogs Sign in | Join | Help
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.

Posted: Sunday, February 24, 2008 5:43 AM by AlexJ
Filed under: , ,

Comments

juliel said:

Based on a previous discussion, you *know* I'll be waiting for that next post! :-)

Julie

# February 24, 2008 8:31 AM

rogerj said:

# February 24, 2008 5:25 PM

MatthieuMEZIL said:

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.

# February 25, 2008 3:52 AM

Matthieu MEZIL said:

J'en avais parlé aux techdays mais je vais profiter du post d' Alex pour en reparler et pour aller un

# February 25, 2008 11:42 AM

MatthieuMEZIL said:

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 ?

# February 25, 2008 11:45 AM

Meta-Me said:

Okay so the 'cat is out of the bag' you CAN do associations with Payloads... Sort of . Now for an explanation

# February 25, 2008 1:36 PM

Meta-Me said:

I'm assuming you've read part 1 and part 2 , if not why not? Just kidding, they will make this post a

# February 25, 2008 10:27 PM

Noticias externas said:

I&#39;m assuming you&#39;ve read part 1 and part 2 , if not why not? Just kidding, they will make this

# February 25, 2008 10:32 PM

Hot Topics said:

If you have a many to many relationship in your database, the Entity Data Model wizard will flatten the

# February 28, 2008 11:26 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker