Update edmx after adding additional column to junction table

1.2k Views Asked by At

I'm using .Net 4.5, entity framework 5, database first. I have a junction (many-to-many) table in my database. For this example lets say the table is "StudentsCourses":

Students
  -PkStudentId
  -Name

Courses
  -PkCourseId
  -CourseNumber

StudentsCourses
  -FkStudentId
  -FkCourseId

This works just fine right now. The 'generate model from database' creates a Student entity with a navigation property to the Course entity. But here is where the trouble is:

I need to add another column to the StudentsCourses table. Lets just call this column "CourseYear". So our junction table would now look like this:

StudentsCourses
  -FkStudentId
  -FkCourseId
  -CourseYear

So, I've added this column to the database and ran "Update Model from Database" on the edmx. I would expect to see an entity created for StudentCourses, with a navigation property to both Students and Courses. But no such entity is created. I still see the same two tables (Students & Courses) with the same navigation property as before.

I've done a lot of reading and researching, but haven't really come across a clear-cut answer. There is a wealth of information on code-first which I can't apply to my scenario. Is there a way to get what I'm after? Is it as simple as adding a PkId to the StudentCourses table? SQL Replication is preventing me from doing this. I would think the composite should suffice, but maybe EF needs a PK to do it's magic? I read a little bit about manually setting relationships, but could not find anything speaking to my particular situation. It could be that I am just missing a simple step in the process of updating the edmx from database. I've done this plenty of times when: adding new tables, adding columns, deleting columns, etc. I'm following the same steps as I always do, but maybe I need to do something different in this case?

Thanks ahead of time for any help. It is greatly appreciated. Please let me know if any more information would help.

2

There are 2 best solutions below

0
On

I'm a bit late for this, but you have the answer in this thread Updating Entity Framework Model after adding a field to a previous look up only table

As they say here, you have to delete the relationship between Students and Courses in the designer. Then update your model from the database, and make sure StudentsCourses table is checked in the Tables branch of the Add tab.

0
On

From what I've gathered it appears as though EF will not generate a model for a table that doesn't have a Primary Key.