mark smith
mark smith

Reputation: 20907

Entity Framework 4.0: Adding a property on a table to hold a entity from another table?

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

Upvotes: 0

Views: 676

Answers (2)

Justin Niessner
Justin Niessner

Reputation: 245429

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.

Upvotes: 1

Scott
Scott

Reputation: 12050

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.

Upvotes: 0

Related Questions