Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to persist enum names #1762

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Dapper/SqlMapper.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static void SetDefaults()
/// instead of the original name; for most scenarios, this is ignored since the name is redundant, but "snowflake" requires this.
/// </summary>
public static bool UseIncrementalPseudoPositionalParameterNames { get; set; }

/// <summary>
/// Whether to persist an enum by its name (<see cref="Enum.ToString()"/>) instead of by its underlying numeric value.
/// </summary>
public static bool PersistEnumsByName { get; set; }
}
}
}
13 changes: 12 additions & 1 deletion Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ public static void SetDbType(IDataParameter parameter, object value)
if (nullUnderlyingType != null) type = nullUnderlyingType;
if (type.IsEnum && !typeMap.ContainsKey(type))
{
type = Enum.GetUnderlyingType(type);
if (Settings.PersistEnumsByName)
type = typeof(string);
else
type = Enum.GetUnderlyingType(type);
}
if (typeMap.TryGetValue(type, out var dbType))
{
Expand Down Expand Up @@ -2247,6 +2250,9 @@ public static object SanitizeParameterValue(object value)
if (value == null) return DBNull.Value;
if (value is Enum)
{
if (Settings.PersistEnumsByName)
return value.ToString();

TypeCode typeCode = value is IConvertible convertible
? convertible.GetTypeCode()
: Type.GetTypeCode(Enum.GetUnderlyingType(value.GetType()));
Expand Down Expand Up @@ -2611,6 +2617,11 @@ internal static Action<IDbCommand, object> CreateParamInfoGenerator(Identity ide
// simplicity, box as Nullable<SomeEnum> and call SanitizeParameterValue
callSanitize = checkForNull = true;
}
else if (Settings.PersistEnumsByName)
{
// When PersistEnumsByName is true, SanitizeParameterValue calls ToString on an enum value.
callSanitize = checkForNull = true;
}
else
{
checkForNull = false;
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
bfriesen marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command

(note: new PRs will not be merged until they add release note wording here)

- Add `Settings.PersistEnumsByName`, to support persisting an enum by its name (`Enum.ToString()`) instead of by its underlying value. (#1762 via bfriesen)

### 2.0.123

- Parameters can now be re-used on subsequent commands (#952 via jamescrowley)
Expand Down