views:

1067

answers:

2

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?

+2  A: 
  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.

Craig Stuntz
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.
Pittsburgh DBA
It is what it is, but most ORMs require editing XML at some point, in my experience.
Craig Stuntz
+3  A: 

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.

David McEwing