How can I manually add a compound association in entity framework?
Asked Answered
P

3

7

This seems like a common case, but for whatever reason, I am stymied by EF's weak editor.

I have two tables, like so:

Table1
(
Column1 int, PK
Column2 int, PK
)

Table2
(
Column1 int, PK, FK
Column2 int, PK, FK
Column3 int, PK
)

Now, the database (out of our control) does not have foreign key constraints. In this example, Table2 is supposed to have a foreign key constraint that references Table1 on (Column1, Column2). In other words, the first two columns of Table2 reference the primary key from Table1. This is an everyday issue in any ordinary database.

In other frameworks, such as LLBLGenPro, we can manually add the relation in a very trivial fashion. However, we cannot figure out how to achieve this in EF's editor. This "Mapping Details" window is awful and non-intuitive. So far, we have managed to select the various tables in the mapping, but it always seems to want to associate on all of the columns.

Any ideas or references?

Pieeyed answered 23/2, 2009 at 22:25 Comment(1)
Did you ever find an answer to this one? I am in the same situation and Craig's solution would take forever and would actually stop us from using EF.Flossie
C
7

Assuming you have the two entities in place.

  1. Right click on Table1 entity header and select add association.
  2. Select Table2 from the right hand end. (Set any other properties in this wizard)
  3. Select the newly created association and edit the mapping. (Should already be selected)
  4. In the Mapping Details window, select Table2

That should be it.

Curbing answered 27/4, 2009 at 4:4 Comment(0)
S
1
  1. Create "temporary" tables with same columns but proper FKs. Use a backup of the DB if you can't alter the "real" one. Don't bother mapping the tables without FKs. It may be easiest if you use a new model, as deleting in the designer doesn't remove store mappings.
  2. Map them with the wizard.
  3. Open the EDMX as XML (not in the designer)
  4. Search and replace the table names, using the tables you really want to map.
  5. Drop the "temporary" tables.

General advice: Edit EDMX manually when doing non-standard things. No FKs = non-standard. The GUI designer only handles certain cases.

Selfimportant answered 24/2, 2009 at 3:29 Comment(2)
Thank you for your reply. This sounds extremely lame. It is not only the FK scenario that will foul this up. There are other instances of custom mappings that people like to add to ORM layers. This does not look good for EF.Pieeyed
It is what it is, but most ORMs require editing XML at some point, in my experience.Selfimportant
P
0
  1. Right click on Table1 entity header and select add association.
  2. Select Table2 from the right hand end. (Set any other properties in this wizard)
  3. Doubleclick the newly created association and set the properties
Pneumatograph answered 22/3, 2012 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.