I have an entity framework application. There is a table that is starting to cause a lot of problems, mostly because it doesn't follow EF conventions:
The two important columns are RequestType
and RequestDetailId
. Each type of request has its own identity column. Since those identities can overlap across tables, there is a RequestType
field to uniquely associate an approval to a request.
I am planning on replacing this "One True Lookup Table" with an Approval
table and a separate many-to-many table for each request table, linking the request to the approval:
I have a couple of requests already pointing to this new approval table and I like how it is working within EF. However, I am having a hard time migrating from the old table to the new tables. So far, I have been looping through the old data with a cursor, inserting into the new Approval
table and using the identity (SELECT SCOPE_IDENTITY()
) to insert into the many-to-many table. I need a cursor because I need to keep track of the approval identity value for each new approval record, so I can populate the many-to-many table with it.
I was curious if someone knew a way to do the two inserts without requiring a cursor. Cursors are fine, but they are hard to read.
One thought was to temporarily add a Request ID field to the Approval
table, inserting the approval with the request ID, then joining that information to the old table to populate the many-to-many table. After the two inserts, I would drop the request ID column. I don't like the idea of adding a nullable request ID column to an existing table just to do a migration.
I was curious if someone could think of a way to split the old lookup table into the new approval/many-to-many tables without using a cursor.