views:

74

answers:

1

Hello, I have two tables that are connected via a join table in a many-to-many relationship in the Entity Framework. I need to add a composite primary key in the join table for the two columns that are related to the joined tables via standard foreign keys but I'm sure how to do that.

+1  A: 

If your join table has a composite key on both foreign keys, the Entity Framework designer in VS2008/2010 should pick up on that.

SQL:

CREATE TABLE MyTable
(
  ColumnFK1  int,
  ColumnFK2  int

PRIMARY KEY (ColumnFK1, ColumnFK2)
)

If the table is already created you can modify the primary key in Management Studio.

Dave Swersky
Thanks Dave, that's what I figured, the designer view of the EDM shows the many-to-many relationship. But when I try to update the related entities, it bugs out with the error: "Unable to update the EntitySet '' because it has a DefiningQuery and no <InsertFunction> element exists"
kingrichard2005
I want to modify it in the Management Studio, but I don't know how to open the .mdf file that my tables are in.
kingrichard2005
Never mind, I was able to open it using instructions from this link: http://answers.yahoo.com/question/index?qid=20071221220201AA9W2mG. Thanks Dave, I was able to resolve the error, what a hassle. Hope my experience can save others time. The following link also helped to shed light on the error I was getting: http://www.mikesdotnetting.com/Article/109/ASP.NET-MVC-Entity-Framework-One-to-Many-and-Many-to-Many-INSERTS
kingrichard2005