Creating a foreign key relationship through the UI in VS2015 SQL Server Database Project
Asked Answered
N

1

6

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?

Nikolai answered 25/3, 2016 at 11:51 Comment(2)
I think you need to update the actual script/TSQL window - there's not a way to do this through the GUI at this time to my knowledge.Kettie
See How to: Use the Table Designer to Manage Tables and Relationships. If that describes what you were doing then the right side is called a Context Pane and the instructions there says what @PeterSchott suspected. However now you have proof.Roundshouldered
L
7
  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
Limicoline answered 5/6, 2017 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.