SetIdentityInsert() public static method

public static SetIdentityInsert ( ObjectName tableName, bool value ) : SqlPreCommandSimple
tableName ObjectName
value bool
return SqlPreCommandSimple
コード例 #1
0
        public static IDisposable DisableIdentity(ObjectName tableName)
        {
            SqlBuilder.SetIdentityInsert(tableName, true).ExecuteNonQuery();

            return(new Disposable(() =>
            {
                SqlBuilder.SetIdentityInsert(tableName, false).ExecuteNonQuery();
            }));
        }
コード例 #2
0
        internal static SqlPreCommand CopyData(ITable newTable, DiffTable oldTable, Replacements rep)
        {
            var selectColumns = newTable.Columns
                                .Select(col => oldTable.Columns.TryGetC(col.Key)?.Name ?? GetDefaultValue(newTable, col.Value, rep))
                                .ToString(", ");

            var insertSelect = new SqlPreCommandSimple(
                $@"INSERT INTO {newTable.Name} ({newTable.Columns.Values.ToString(a => a.Name, ", ")})
SELECT {selectColumns}
FROM {oldTable.Name}");

            if (!newTable.PrimaryKey.Identity)
            {
                return(insertSelect);
            }

            return(SqlPreCommand.Combine(Spacing.Simple,
                                         SqlBuilder.SetIdentityInsert(newTable.Name, true),
                                         insertSelect,
                                         SqlBuilder.SetIdentityInsert(newTable.Name, false)
                                         ));
        }
コード例 #3
0
        public static IDisposable DisableIdentity(Table table)
        {
            if (!table.IdentityBehaviour)
            {
                throw new InvalidOperationException("Identity is false already");
            }

            table.IdentityBehaviour = false;
            if (table.PrimaryKey.Default == null)
            {
                SqlBuilder.SetIdentityInsert(table.Name, true).ExecuteNonQuery();
            }

            return(new Disposable(() =>
            {
                table.IdentityBehaviour = true;

                if (table.PrimaryKey.Default == null)
                {
                    SqlBuilder.SetIdentityInsert(table.Name, false).ExecuteNonQuery();
                }
            }));
        }