1

I'm having issues adding a Foreign Key to link 2 existing tables together. Table A has data, and I need it to reference Table B (which also has data).

I will need to insert a row (or rows) into Table B which Table A will reference.

In this case it is acceptable to insert a row into Table B and then use that as the default value for the migration. That would require that I know the ID of the row that I'm inserting.

I think that I can handle everything except figuring out the ID of the row that I insert into Table B.

Is it possible to return data inside of Migrations?

thepaulpage
  • 4,614
  • 2
  • 25
  • 39
  • I don't fully understand your question, there is a very simple way to add a foreign key inside a migration `AddForeignKey("TableA", "TableB_Id", "TableB", "Id");` you could also do it in SQL by running `SQL("[your query goes here]")` but it is void, so no result is to be expected – Luiso May 19 '15 at 21:18
  • I edited my question. I know how to add the foreign key, but I don't know how to add it with existing (or even new!) data – thepaulpage May 20 '15 at 13:30
  • 1
    I think you will have to use SQL to do what you want – Luiso May 20 '15 at 17:16
  • You could try scope_identity() http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row http://community.sharpdevelop.net/blogs/mattward/archive/2013/12/23/EntityFramework6CodeFirstMigrations.aspx – Steve Greene May 21 '15 at 17:08
  • That's basically exactly what I'd like to do, but I don't think I can run queries inside of a DbMigration file – thepaulpage May 21 '15 at 19:40

0 Answers0