Returns a SQL-DDL statement for creating a foreign key constraint corresponding to the association.

Namespace:  Huagati.DBMLTools.Runtime
Assembly:  HuagatiDBMLToolsRTE (in HuagatiDBMLToolsRTE.dll) Version: 1.66.3362.23798

Syntax

C#
public static string AddFKStatement(
	this MetaAssociation association
)
Visual Basic (Declaration)
<ExtensionAttribute> _
Public Shared Function AddFKStatement ( _
	association As MetaAssociation _
) As String
Visual C++
[ExtensionAttribute]
public:
static String^ AddFKStatement(
	MetaAssociation^ association
)

Parameters

association
Type: System.Data.Linq.Mapping..::.MetaAssociation
Linq-to-SQL MetaAssociation representing the association/foreign key to generate a SQL-DDL 'alter table ... add constraint' statement for.

Return Value

SQL-DDL statement for adding a foreign key constraint corresponding to the association.

Examples

This example shows how to check whether a foreign key corresponding to an association exists in the database, and generate SQL-DDL for adding missing constraints.
CopyC#
using Huagati.DBMLTools.Runtime;

public class MappingSample
{
    public void Test()
    {
        SomeDataContext dc = new SomeDataContext();

        //iterate through the collection of tables defined in the datacontext
        foreach (System.Data.Linq.Mapping.MetaTable table in dc.Mapping.GetTables())
        {
            //iterate through the collection of associations
            foreach (System.Data.Linq.Mapping.MetaAssociation association in table.RowType.Associations)
            {
                if (!association.ExistsInDB(dc.Connection))
                {
                    //foreign key constraint does not exist
                    string addFK = association.AddFKStatement();
                }
            }
        }
    }
}

See Also