22// System : EWSoftware Entity Framework Utilities
33// File : DatabaseExtensions.cs
44// Author : Eric Woodruff
5- // Updated : 09/05 /2025
5+ // Updated : 10/17 /2025
66//
77// This file contains a class that contains extension methods for database objects
88//
@@ -128,6 +128,15 @@ private static (DbConnection connection, DbCommand command, Dictionary<string, P
128128 var connection = dataContext . Database . GetDbConnection ( ) ;
129129 var command = connection . CreateCommand ( ) ;
130130
131+ // If any properties have a column name attribute, add an entry for the alias
132+ foreach ( var p in properties . Values . ToList ( ) )
133+ {
134+ var cn = p . GetCustomAttribute < ColumnAttribute > ( ) ;
135+
136+ if ( cn != null )
137+ properties [ cn . Name ! ] = p ;
138+ }
139+
131140 command . CommandType = CommandType . StoredProcedure ;
132141 command . CommandText = CreateStoredProcedureName ( dataContext , storedProcName ! ) ;
133142
@@ -150,8 +159,9 @@ private static (DbConnection connection, DbCommand command, Dictionary<string, P
150159 if ( ! properties . TryGetValue ( key , out var p ) )
151160 throw new InvalidOperationException ( $ "The key property { key } was not found on the entity") ;
152161
153- // If the property has a column attribute, use the name from it instead
154- var columnName = p . GetCustomAttribute < ColumnAttribute > ( ) ;
162+ // If the property has a parameter name or column attribute, use the name from it instead
163+ string ? columnName = p . GetCustomAttribute < ParameterNameAttribute > ( ) ? . Name ??
164+ p . GetCustomAttribute < ColumnAttribute > ( ) ? . Name ;
155165
156166 object ? value = parameters [ paramIdx ++ ] ;
157167
@@ -171,7 +181,7 @@ private static (DbConnection connection, DbCommand command, Dictionary<string, P
171181
172182 // If the parameter value is null, use DBNull.Value to send a NULL to the database rather than
173183 // using any default value assigned to the parameter.
174- var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ? . Name ?? key } ",
184+ var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ?? key } ",
175185 value ?? DBNull . Value ) ;
176186 param . SetParameterType ( p . PropertyType ) ;
177187 command . Parameters . Add ( param ) ;
@@ -244,13 +254,14 @@ private static (DbConnection connection, DbCommand command,
244254
245255 if ( ( forInsert && ! ( ignored ? . ForInsert ?? false ) ) || ( ! forInsert && ! ( ignored ? . ForUpdate ?? false ) ) )
246256 {
247- // If the property has a column attribute, use the name from it instead
248- var columnName = p . GetCustomAttribute < ColumnAttribute > ( ) ;
257+ // If the property has a parameter name or column attribute, use the name from it instead
258+ string ? columnName = p . GetCustomAttribute < ParameterNameAttribute > ( ) ? . Name ??
259+ p . GetCustomAttribute < ColumnAttribute > ( ) ? . Name ;
249260 var timestamp = p . GetCustomAttribute < TimestampAttribute > ( ) ;
250261
251262 // If the parameter value is null, use DBNull.Value to send a NULL to the database rather than
252263 // using any default value assigned to the parameter.
253- var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ? . Name ?? p . Name } ",
264+ var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ?? p . Name } ",
254265 p . GetValue ( entity ) ?? DBNull . Value ) ;
255266 param . SetParameterType ( p . PropertyType ) ;
256267
@@ -310,12 +321,13 @@ private static (DbConnection connection, DbCommand command, bool neverTrack)
310321 if ( ! properties . TryGetValue ( key , out var p ) )
311322 throw new InvalidOperationException ( $ "The key property { key } was not found on the entity") ;
312323
313- // If the property has a column attribute, use the name from it instead
314- var columnName = p . GetCustomAttribute < ColumnAttribute > ( ) ;
324+ // If the property has a parameter name or column attribute, use the name from it instead
325+ string ? columnName = p . GetCustomAttribute < ParameterNameAttribute > ( ) ? . Name ??
326+ p . GetCustomAttribute < ColumnAttribute > ( ) ? . Name ;
315327
316328 // If the property value is null, use DBNull.Value to send a NULL to the database rather than
317329 // using any default value assigned to the parameter.
318- var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ? . Name ?? key } ",
330+ var param = new SqlParameter ( $ "@{ parameterNamePrefix } { columnName ?? key } ",
319331 p . GetValue ( entity ) ?? DBNull . Value ) ;
320332 param . SetParameterType ( p . PropertyType ) ;
321333 command . Parameters . Add ( param ) ;
@@ -522,8 +534,9 @@ private static async Task<int> InsertUpdateEntityInternalAsync<TEntity>(DbContex
522534 {
523535 var mp = methodParams [ idx ] ;
524536
525- // If the parameter has a column attribute, use the name from it instead
526- var columnName = mp . GetCustomAttribute < ColumnAttribute > ( ) ;
537+ // If the parameter has a parameter name or column attribute, use the name from it instead
538+ string ? columnName = mp . GetCustomAttribute < ParameterNameAttribute > ( ) ? . Name ??
539+ mp . GetCustomAttribute < ColumnAttribute > ( ) ? . Name ;
527540
528541 object ? value = parameters [ idx ] ;
529542 var paramType = mp . ParameterType . IsByRef ? mp . ParameterType . GetElementType ( ) ! : mp . ParameterType ;
@@ -544,7 +557,7 @@ private static async Task<int> InsertUpdateEntityInternalAsync<TEntity>(DbContex
544557
545558 // If the parameter value is null, use DBNull.Value to send a NULL to the database rather than
546559 // using any default value assigned to the parameter.
547- var p = new SqlParameter ( $ "@{ spName ? . ParameterNamePrefix ?? contextParamPrefix ? . Prefix } { columnName ? . Name ?? mp . Name } ",
560+ var p = new SqlParameter ( $ "@{ spName ? . ParameterNamePrefix ?? contextParamPrefix ? . Prefix } { columnName ?? mp . Name } ",
548561 value ?? DBNull . Value ) ;
549562 p . SetParameterType ( paramType ) ;
550563
0 commit comments