views:

241

answers:

2

Hi there,

I wanted to create a new property on a table in my model.. Basically i have a table called contract which has lots of fields, but i want to add another field called client which will hold my other table called client..

I tried playing around with complex types but i don't know if this is it.... Then i found navigation property ... this sounded interesting ... like a link to the client maybe?? .. but i could seem to point it anywhere..

Basically the Contract table/model needs a new property called client ... client is actually an entity but the client must travel whith the contract.

How is this done??

I wanted to set this up in the model so i can automatically UPDATE Model etc.... and not loose any custom changes..

I could of course just insert something into the Partial class on another file....

Any ideas?

Thanks

+1  A: 

What you're trying to do is usually accomplished by properly defining the primary/foreign key relationships between the tables in the database (or between the fields in your Model, if you're doing Model Driven Design).

Entity Framework will auto-magically see the foreign key relationship between the tables and generate a property on the parent class to hold the child.

As for your comment (which is better, defining the relationship in the model or creating partial classes):

It's far better to define the relationship in your Model to implement in partial classes. Having the relationship defined in your Model will enforce integrity even if your database doesn't...whereas if you were using partial classes, you'd have to code the enforcement yourself.

Justin Niessner
Hi Justin, ermm so if i code manual in a partial class what would i be missing exactly?? Referential integrity??? or something else?
mark smith
Yes. Referential Integrity would be missing. Like I said, you could manually code it...but why waste the effort?
Justin Niessner
A: 

If possible, I think you'll want to add a foreign key to your contract table that holds the primary key of your client. Then you can click "update model" which will add a navigation property to your model.

Scott
Hi scott... yes but i don't want to put any changes in the db itself, can i add the foreign key just on the model?
mark smith
Yes it appears i can add it manually!
mark smith
Glad to see it works!
Scott