Exemple #1
0
        public override void Assign(DatabaseObjectInfo source)
        {
            base.Assign(source);

            var src = (ForeignKeyInfo)source;

            OnUpdateAction = src.OnUpdateAction;
            OnDeleteAction = src.OnDeleteAction;
            RefTableName   = src.RefTableName;
            RefSchemaName  = src.RefSchemaName;

            foreach (var col in src.RefColumns)
            {
                RefColumns.Add(col.Clone());
            }
        }
Exemple #2
0
        public string ScriptCreate()
        {
            AssertArgNotNull(Table, "Table");
            AssertArgNotNull(Columns, "Columns");
            AssertArgNotNull(RefTable, "RefTable");
            AssertArgNotNull(RefColumns, "RefColumns");

            var text           = new StringBuilder();
            var constraintName = IsSystemNamed ? string.Empty : $"CONSTRAINT [{Name}]";

            text.Append(
                $"ALTER TABLE [{Table.Owner}].[{Table.Name}] WITH {CheckText} ADD {constraintName}\r\n");
            text.Append(
                $"   FOREIGN KEY([{string.Join("], [", Columns.ToArray())}]) REFERENCES [{RefTable.Owner}].[{RefTable.Name}] ([{string.Join("], [", RefColumns.ToArray())}])\r\n");
            if (!string.IsNullOrEmpty(OnUpdate) && !_defaultRules.Split('|').Contains(OnUpdate))
            {
                text.Append($"   ON UPDATE {OnUpdate}\r\n");
            }

            if (!string.IsNullOrEmpty(OnDelete) && !_defaultRules.Split('|').Contains(OnDelete))
            {
                text.Append($"   ON DELETE {OnDelete}\r\n");
            }

            if (!Check && !IsSystemNamed)
            {
                text.Append(
                    $"   ALTER TABLE [{Table.Owner}].[{Table.Name}] NOCHECK CONSTRAINT [{Name}]\r\n");
            }

            return(text.ToString());
        }