コード例 #1
0
        /// <summary>
        /// Creates a command factory which can be used to create entities for multiple inserts each returning the primary key.
        /// </summary>
        public static string MakeInsertRangeCommand <TEntity, TPrimaryKey>(IEnumerable <TEntity> entities, IDialect dialect)
        {
            Ensure.NotNull(entities, nameof(entities));
            var config = MicroCRUDConfig.Current;

            dialect = dialect ?? config.Dialect;

            var tableSchema = TableSchemaFactory.GetTableSchema(typeof(TEntity), dialect, config.SchemaFactory);

            if (!tableSchema.CanGeneratePrimaryKey(typeof(TPrimaryKey)))
            {
                throw new InvalidPrimaryKeyException(
                          "InsertRange<TEntity, TPrimaryKey>() can only be used for Int32 and Int64 primary keys. Use InsertRange<TEntity>() for other types of primary keys.");
            }

            return(dialect.MakeInsertReturningIdentityStatement(tableSchema));
        }
コード例 #2
0
        /// <summary>
        /// Creates a command which will insert an entity, returning the primary key.
        /// </summary>
        public static CommandDefinition MakeInsertReturningPrimaryKeyCommand <TPrimaryKey>(
            object entity,
            IDbTransaction transaction,
            IDialect dialect,
            int?commandTimeout,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.NotNull(entity, nameof(entity));
            var config = MicroCRUDConfig.Current;

            dialect = dialect ?? config.Dialect;

            var tableSchema = TableSchemaFactory.GetTableSchema(entity.GetType(), dialect, config.SchemaFactory);

            if (!tableSchema.CanGeneratePrimaryKey(typeof(TPrimaryKey)))
            {
                throw new InvalidPrimaryKeyException(
                          "Insert<TPrimaryKey>() can only be used for Int32 and Int64 primary keys. Use Insert() for other types of primary keys.");
            }

            var sql = dialect.MakeInsertReturningIdentityStatement(tableSchema);

            return(MakeCommandDefinition(sql, entity, transaction, commandTimeout, cancellationToken));
        }