Ad

Creating A Foreign Key Relationship Through The UI In VS2015 SQL Server Database Project

I added a new SQL Server database project to my solution in VS2015. I added all of my tables to this project.

I am now trying to figure out how to add foreign keys to the project through the UI preferably .

I am in the table designer and on the right side I click foreign keys and then add new. This adds a new foreign key, but when I go to the properties of it I am unable to set the columns and tables. Everything is grayed out.

The T-SQL looks like this:

CONSTRAINT [FK_Product_ToTable] 
     FOREIGN KEY ([Column]) REFERENCES [ToTable]([ToTableColumn]) 

I know I can just simply replace the params with the correct info in the T-SQL pane, but is there any way to completely do it through the UI and let the T-SQL update on its own?

Ad

Answer

  1. double click your table in solution explorer to edit the columns
  2. add FK column to the table, optionally check "Allow Nulls"
  3. to the right of the table column list, right click "Foreign Keys"
  4. a menu will appear with an item "Add New Foreign Key"
  5. click that item, and you will see it add a new FK constraint under the "Foreign Keys" list. Rename the FK to your liking, then press ENTER.
  6. This will add a FK SQL statement in the "T-SQL" window below the table column list.
  7. edit the SQL script to define the "Column", "ToTable", and "ToTableColumn" for your foreign key. enter image description here
Ad
source: stackoverflow.com
Ad