Search code examples
codefluent

SQL Producer - PK and FK constraint naming


We encountered trouble on our model while running pivot file. It appears that we suffered from the naming collision on our PK and FK's (like in this).

Our use case was such that we created new table (say tableAB), but because of the naming collision, the PK (say PK_tableA) wasn't created because another table (say tableAB) had already this PK (PK_tableA) defined. The pivot would stop processing because of a FK referencing non-existing PK on tableAB.

Is there any way to rename PK and FK constraint ? In particular we already use a custom class overriding SoftFluent.Samples.CustomNamingConvention.FormatNamingConvention, but this only seems to apply to DF_ constraints (see here). I would like to achieve same things with PK and FK constraints, what is the best solution ?

EDIT 08/07/2016

Finally found my way with the object casting. This code below generates the constraint with the expected naming:

 public override string GetShortName(IShortNamedObject obj, IDictionary context)
 {
        Object objCopy = obj;
        if (objCopy is Constraint)
        {
            Constraint constraint = objCopy as Constraint;
            if (constraint != null && constraint.Table != null)
            {
                if (constraint.ConstraintType == ConstraintType.PrimaryKey || constraint.ConstraintType == ConstraintType.ForeignKey)
                {
                    var result = constraint.Table.Name + base.GetShortName(obj, context);
                    return result;
                }
            }

        }
        return base.GetShortName(obj, context);
 }

Thanks for the support provided.


Solution

  • A naming convention can support many different types of names (full, short, display, etc.) depending on the type of abstract concept (constraint, table, procedure, etc.) processed by the inference pipeline, this is how is defined CodeFluent's INamingConvention (in the CodeFluent.Model.Common.Naming namespace):

    public interface INamingConvention
    {
        string GetName(INamedObject obj, IDictionary context);
        string GetShortName(IShortNamedObject obj, IDictionary context);
        string GetFullName(IFullNamedObject obj, IDictionary context);
        string GetDisplayName(IDisplayNamedObject obj, IDictionary context);
        string GetClrFullTypeName(IClrFullTypeNamedObject obj, IDictionary context);
        string GetPersistenceName(IPersistenceNamedObject obj, IDictionary context);
        string GetPersistenceShortName(IPersistenceShortNamedObject obj, IDictionary context);
    }
    

    Constraints use short names and full names, so if you derive from FormatNamingConvention (which implements INamingConvention), you should override the GetShortName and/or GetFullName methods and cast obj as a Constraint.

    Note it is unusual to have so many problems with short names, or maybe all your entities start with the same names over and over accross model versions. You can also tweak the short name length (wich is by default set to 3) with the persistenceShortNameLength attribute: CodeFluentRuntimeException (CF1024): Cannot determine short name