views:

852

answers:

2

For a variety of reasons the database that I'm working on (SQL Server 2005) doesn't have any relationships defined. Every table has a primary key. And most tables have at least one foreign key, however we've never configured the constraints.

Can anyone tell me the steps that I should take to inform Entity Framework of the underlying relationships between tables? Is there a particular document that describes this process?

+1  A: 

You will need to manually create the associations between the tables in your EF model.

In the Entity Framework designer surface, you basically right-click on your table and from the context menu, you need to choose the "Add -> Association" option. In the dialog box that pops up, you can establish the association between your two tables - even without foreign key relationship in the underlying database.

Marc

marc_s
Using this method seems to provide only a superficial relationship between entities. The objects are connected, but at the storage level Entity-Framework still has no idea which FK field points to the PK.
zorlack
Yes of course - it would be much better to actually add those foreigny key constraints to the database and use them in your EF model.
marc_s
I was assuming from how you posted your question that you probably couldn't do that and just wanted to find a way to at least provide some relationships in the EF model.
marc_s
It seems like the correct answer is to manually insert the association in the SSDL schema as per the cribsheet that you originally posted.http://www.simple-talk.com/dotnet/.net-framework/entity-framework-the-cribsheet/The important piece that I was missing was that I had to manually delete UserID from the child table.
zorlack
Just a word of caution - fiddling around with the SSDL might prove a bit dangerous, since the SSDL should really reflect the actual schema as it exists on the database. Changing that manually seems a bit tricky to me - your actual reality and the SSDL suddenly aren't in sync anymore.... Either really change the database (my preferred option), or then just use the "superficial" associations in the conceptual level.
marc_s
A: 

Hi guys,

I am using .Net 2008 and Sql Server 2008.

I have the following tables.

'------------------
    Account
'-----------------
ID

Name

Version

'-----------------

PK - ID + Version

'============================

'------------------
    Details
'-----------------
ID

AccountID

Version

'-----------------

PK - ID + Version

FK - AccountID + Version TO Account.ID + Account.Version

'============================

Now My Question is

How I can added this FK Association in the .NET Entity Framework model. I generate the Entity Framework model from wizard but it does on the add the FK relationship association.

I know that it works in the Visual Studio 2010 Framework 4.0 but I need resolution on this in the Visual Studio 2008.

I will appriciate if you some give snap or hint on this so that i can get it right.

Thanks

Kamleshkumar Gujarathi

[email protected]

NJ