Skip to content

Instantly share code, notes, and snippets.

View tcartwright's full-sized avatar

Tim Cartwright tcartwright

  • Houston, Texas
View GitHub Profile
@tcartwright
tcartwright / EFCommandInterceptor.cs
Last active May 29, 2024 15:09
C#: EFCommandInterceptor - Intercepts EF commands and tags them with the class.method(parameters).linenumber. Even action queries, while .TagWithCallSite() only does Selects
using System.Data.Common;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace ~~~~~;
public class EFCommandInterceptor : DbCommandInterceptor
{
public override InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result)
@tcartwright
tcartwright / ObjectExtensions.cs
Created May 14, 2024 15:39
C#: IsDisposed() Checks to see if an object is disposed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
public static class ObjectExtensions
{
@tcartwright
tcartwright / UpdatePackages.bat
Last active May 10, 2024 14:58
DOTNET: Update all packages in a solution
dotnet restore --no-cache --force
OR
nuget restore -nocache -force
LAST RESORT, CLEAR THE LOCAL CACHE:
nuget locals all -clear
@tcartwright
tcartwright / SimpleMaintenance.sql
Created February 15, 2024 21:00
SQL SERVER: Simple maintenance scripts
/*****************************************************************************************************************************************/
/**** SHRINK LOG FILES *******************************************************************************************************************/
/*****************************************************************************************************************************************/
DECLARE @sql VARCHAR(max) = ''
SELECT @sql += CONCAT('USE [', DB_NAME([mf].[database_id]), ']', CHAR(13), CHAR(10), 'DBCC SHRINKFILE (''', [mf].[name], ''', 1) WITH NO_INFOMSGS;', CHAR(13), CHAR(10))
FROM sys.[master_files] AS [mf]
WHERE [mf].[type_desc] = 'LOG'
@tcartwright
tcartwright / GitPopCommit.bat
Created January 16, 2024 22:51
GIT: Pop last commit off
@rem remove last commit and destroy it
git reset --hard HEAD~1
@rem remove last commit and edit it
git reset HEAD~1
@tcartwright
tcartwright / NugetCacheClear.bat
Created January 5, 2024 21:01
NUGET: Clears all local caches
@REM https://docs.nuget.org/consume/command-line-reference
@call "%ProgramFiles%\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat"
nuget locals all -clear
@tcartwright
tcartwright / MoveGitProject.cmd
Last active January 3, 2024 19:35
GIT: Moves a project from one repo to another preserving history
@REM Make sure you are in the root folder of the git repo that you wish to move the code to
git checkout master
git remote add r1remote **url-of-repo1**
git fetch r1remote
git merge r1remote/master --allow-unrelated-histories
git remote rm r1remote
@tcartwright
tcartwright / MyContextReadOnly.cs
Last active January 3, 2024 19:33
C#: Read only configuration context for sql server
public partial class MyContextReadOnly : MyContext
{
private readonly string _connectionString = null;
public MyContextReadOnly(DbContextOptions<MyContextReadOnly> options) : base(options)
{
var sqlServerOptionsExtension = options.FindExtension<SqlServerOptionsExtension>()!;
_connectionString = sqlServerOptionsExtension?.ConnectionString!;
}
@tcartwright
tcartwright / RunTimeStringInterprolation.cs
Last active December 1, 2023 15:25
C#: Run time interprolation
public class NamedParameter
{
public string Name { get; set; } = null!;
public object Value { get; set; }
public NamedParameter(string name, object value)
{
if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(nameof(name)); }
if (value == null) { throw new ArgumentNullException(nameof(value)); }
Name = name;
Value = value;
@tcartwright
tcartwright / TVCCreate.sql
Created September 18, 2023 17:36
SQL SERVER: TVC: Create table variable from query
/**
Copyright (c) Red Gate Software Ltd 2021
All rights Reserved. Use of this code is subject to the terms of a license agreement with Red Gate Software Limited.
This is a temporary batch for creating such things as table variables, temporary tables or anything else that needs a column list.
For more info, see https://www.red-gate.com/hub/product-learning/sql-prompt/building-reusable-table-build-scripts-using-sql-prompt
**/