I had a similar problem some time ago. Here is a script I wrote using the Sql Server SMO:
public static string GetForeignKeyScript()
{
SqlConnection conn = new System.Data.SqlClient.SqlConnection("SOME_CONNECTION_STRING");
Server server = new Server(new ServerConnection(conn));
Database db = server.Databases["SOME_DATABASE"];
Table Roles = db.Tables["SOME_TABLE"];
var sb = new StringBuilder();
foreach (Table table in db.Tables)
foreach (ForeignKey fk in table.ForeignKeys)
foreach (string s in fk.Script())
sb.AppendLine(s);
return sb.ToString();
}
This will output a string containing the scripts for creating the foreign keys.
Edited the code to remove stuff that was fairly specific to my project (I was only interested in tables that ended in a certain string for instance)