+5  A: 

You have to select the table, that cotains the foreign key. The next step is to remove that foreign key from the entity, as it is already expressed using the relation you just created.

Example:

table A
-------
A_ID int
B_ID int


table B
-------
B_ID int

In this case, you would select the table A in the designer, as it contains the foreign key. Also you'll need to remove B_ID from the A entity afterwards.

driAn
A: 

Thanks driAn. This is getting me closer. The problem is that the foreign key (e.g. A.B_ ID) is also the primary key of the table, and if I delete it from the entity (A) I get another error:

Error 159: EntityType 'ClaimsModel.DW_WF_ClaimInfo' has no key defined. 
Define the key for this EntityType.

Can a property serve as both the primary key for an entity and a foreign key to another entity? If so, how?

Craig Fisher
How did you end up solving this problem?
A: 

I have the same problem. Any solutions?

A: 

Not shure to have the answer, but with Entity Framework, I always create a primary key in each table (even if I don't need it). Example :

  • Table Customer have CustumerID as primary key
  • Table Product have ProductID as primary key
  • Table Order of course use CustomerID + ProductID as the primary key. Well, I also create a single local primary key : OrderID.
Sylvain
A: 

I was dealing with a legacy app so adding an additional primary key was not an option for me. What I had to do was map it as a 1:(0 or 1) rather than a 1:1 to get it to work. For example if I had two tables say Customer and CustomerDetails that both had a primary key called CustomerID. To create the association, I had to set it up as 1 Customer can relate to (0 or 1) CustomerDetails records. Eveytime you Insert a Customer, make sure to also insert the CustomerDetails so you can maintain the 1:1.

Mike
A: 

I agree that the way this is set up seems counter-intuitive. For anyone that is too slow (like me) to get driAn's answer: Per the forum post below, I remembered that we are dealing with an entity, not a table directly. The association is on the entity (which could model the table, but doesn't have to).

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/

The entity framework is associating PROPERTIES to TABLE COLUMNS. That is why we must delete the "foreign key" PROPERTY on the ENTITY (note: we are not deleting the table column). To reference the column in code, (using the ENTITY modeling the given example of TABLE A & TABLE B) you would write something like this:

variable_A = tableAs.A_ID
variable_B = tableAs.tableBs.B_ID

The second assignment is using the association defined in the entity to get to the data. There is no PROPERTY called "B_ID" on table A.

That is all assuming I understand it right :). It is at least intelisensing correctly for me now.

:-Dan

Watki02
A: 

first create foriegn key in the database and then create association or update model from database

A: 

In Entity Framework, there should be no foreign keys in the conceptual design. I believe this is now allowed in EF4 (with some tweaking), but in EF3.5, it can't be done.

To fix this, simply delete all properties which represent foreign keys in the EF-designer. Do not delete the primary keys!

If you then get an "Association end is not mapped.." error, see (my answer to) this post.

BlueRaja - Danny Pflughoeft