views:

301

answers:

2

Let's say I have two tables in SQL that I want to have an EDMX model generated from (for now, going with automagical generation for everything) using POCO T4 templates. Let's say our two tables are Person (ID, FName, LName) and Comment (ID, PersonID, CommentText, CommentDate) with a one-to-many relation between the two (i.e. you can make many comments about one person).

Getting my POCO entities generated from this is trivial and works beautifully. What I don't know how to do now, though, is to add a custom navigation property on my Person entity that represents the most recent comment for that person (eventually something even more complex than this will ultimately be needed). For now, it's okay that it's read-only but it would be nice to know how to handle an writable property too.

What is the proper way to do this? One thing to consider is that I'm serializing these entities so I will need them to be eager-loaded and persisted in a way that I can push them out to my UI with WCF in the middle (i.e. a custom hand-written property in an extension class that relies on lazy loading is not an option).

I've gotten pretty good at using EF4 for standard stuff, but now that I'm getting into this custom stuff, I'm not entirely sure how to do this in a best practice way.

+1  A: 

Adding a property is easy. Put it in a partial class, named the same as the entity. But if your property looks like:

get
{
     return this.Comments.OrderByDescending(c => c.CommentDate).FirstOrDefault();
}

...then it will work but you can't use it in an L2E query.

If you need L2E support, you can use this technique.

Craig Stuntz
The sample you provided won't work because I need this for L2E queries AND for the value to be serialized. I'll look into your other technique that you mentioned. BTW, thanks for stalking me and my questions and for all of the answers you're providing. ;-)
Jaxidian
Looking into the other solution looks pretty involved (not necessarily in consumption but at least in understanding what's going on). As such, it's hard to have a gut feeling for how well it will work. Will it work with eager loading, explicit lazy loading, and in being serialized? And is there really not a way for me to do this within my EDMX mapping? I thought I was supposed to be able to have "interesting" mappings take place there so I can benefit from SQL optimized queries with those "interesting" mappings...
Jaxidian
IMHO your entity is the wrong place to customize serialization. Adding custom properties to an entity is *different* than mapping. Yes, you can do "interesting" mappings to some degree (e.g., entity splitting), but custom properties is something else entirely.
Craig Stuntz
Jaxidian
Unfortunately, I've been unable to use this method in a way where it provides Eager Loading.
Jaxidian
A: 

Hi Jaxidian, have you found a solution to your problem? Like you, I'd like to add a custom property on a POCO with eager loading.

For example, I'd like to have the possibility to laod the latest comments of the persons when I retrieve the person :

var person = context.Person.Include("LatestComments").SingleOrDefault(p => p.id = 1);

How can we add this property?

THX!

Warrick