views:

64

answers:

1

I was wanting to know if there is a good way to work in timestamps into junction tables using the Entity Framework (4.0). An example would be ...

Clients
--------
ID   | Uniqueidentifier
Name | varchar(64)

Products
-----------
ID   | uniqueidentifier
Name | varchar(64)

Purchases
--------
Client | uniqueidentifier
Product | uniqueidentifier

This works smooth for junctioning the two together - but I'd like to add a timestamp. Whenever I do that, I'm forced to go through the middle-table in my code. I don't think I can add the timestamp field to the junction table - but is there a different method that might be useable?

+1  A: 

Well, your question says it all. You must either have a middle "Purchases" entity or not have the timestamp on Purchases. Actually, you can have the field on the table if you don't map it, but if you want it on your entity model then these are the only two choices.

Craig Stuntz
Sigh. I was afraid of that, I just hoped that a better developer than myself might know something I didn't. Thank you for the help.
Stacey
Can you explain how I would go about adding the field and not mapping it? It sort of force-maps everything.
Stacey
The easiest way (we actually do this) is to maintain a separate copy of the DB metadata without the properties in question. You *can* do the mapping manually, and use the same DB, but we find this approach *far* simpler since creating DB metadata without the problematic fields is trivial, and mapping manually is much more work.
Craig Stuntz
you have already helped me a lot - so I'll understand if you're not interested - but I am a little confused. I've never done manual mapping with EF. Would I build the map, delete the entity, then rewrite it manually? Do you know of any simple examples that might point me in the right direction? I don't need the timestamp in the entuty, just the data. Though I am curious as to how the value would get set.
Stacey
also, is what I am trying to do considered a bad practice? It seems like the obvious way to join the tables and track it - but it isn't supported by default. Am I trying something that isn't supported for any specific reason I'm just too inexperienced to see?
Stacey
I don't think what you're doing is wrong; some ORMs allow properties on associations. The EF doesn't have that feature. If you don't map these fields, though, you will not be able to see them in the EF, so make sure that's what you want before you follow this route. In our case the "extra" fields aren't needed in this app. Otherwise map the fields and live with the "extra" entity. To map, we just set the app.config to a DB without the "extra" fields, then change that to the "real" DB at runtime.
Craig Stuntz
I'm coding in MVC, and it suddenly occured to me - why not apply the same principle as ViewModels? Take the Purchases Table, which references a product, and add a Date Field to it, and then Junction the Purchases instead of the Products. Off hand is there any really bad reason why this isn't a good idea?
Stacey
That seems OK to me, though I don't really know much about your app. Do whatever seems simplest and most maintainable to you.
Craig Stuntz