public dynamic UpdateSingle <T>(string updateSql, IDictionary <string, object> parameterList, T entity) where T : IDataModel
        {
            var modelType = typeof(T);
            var tableName = modelType.Name;
            var idColumn  = DataModelHelper.GetPrimaryKeyProperty(modelType);

            dynamic record = null;

            using (var connection = this.GetConnection())
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }

                var affectedRows = connection.Execute(updateSql, parameterList);
                if (affectedRows == 0)
                {
                    DatabaseExceptionHelper.ThrowConcurrencyExcepiption(entity.Id);
                }

                var idQuery = string.Format("SELECT {1} AS Id, RowVersion FROM {0} WHERE {1} = @{1}", tableName, idColumn);
                record = connection.Query(idQuery, entity).Single();
            }

            return(record);
        }
Esempio n. 2
0
        public SqlDelete(T entity)
        {
            this.entity = entity;
            var modelType = typeof(T);

            this.tableName        = modelType.Name;
            this.primaryKeyColumn = DataModelHelper.GetPrimaryKeyProperty(modelType);
        }
Esempio n. 3
0
        public SqlUpdate(T entity)
        {
            this.entity = entity;
            var modelType = typeof(T);

            this.tableName        = modelType.Name;
            this.primaryKeyColumn = DataModelHelper.GetPrimaryKeyProperty(modelType);
            this.columnNames      = DataModelHelper.GetProperties(modelType).Where(x => x != primaryKeyColumn);
        }
        public void DeleteSingle <T>(string deleteSql, IDictionary <string, object> parameterList, T entity) where T : IDataModel
        {
            var modelType = typeof(T);
            var tableName = modelType.Name;
            var idColumn  = DataModelHelper.GetPrimaryKeyProperty(modelType);

            using (var connection = this.GetConnection())
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }

                var affectedRows = connection.Execute(deleteSql, parameterList);
                if (affectedRows == 0)
                {
                    DatabaseExceptionHelper.ThrowConcurrencyExcepiption(entity.Id);
                }
            }
        }
        public dynamic Insert <T>(string insertSql, T entity) where T : IDataModel
        {
            var modelType = typeof(T);
            var tableName = modelType.Name;
            var idColumn  = DataModelHelper.GetPrimaryKeyProperty(modelType);

            var insertSqlReturningId = string.Format("{0}; SELECT {2} AS Id, RowVersion FROM {1} WHERE {2} = CAST(SCOPE_IDENTITY() as int)", insertSql, tableName, idColumn);

            dynamic record = null;

            using (var connection = this.GetConnection())
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                }

                record = connection.Query(insertSqlReturningId, entity).Single();
            }

            return(record);
        }