Dapper: Store Enum Value as String

Created on 4 Jul 2017  路  18Comments  路  Source: StackExchange/Dapper

I have a simple enum just like this:

public enum Foo
{
  First,
  Second,
  Third
}

When saving in the dabase, the value are stored as the indexed position of the enum value.
Is possible to store an enum property as string in the database?

enhancement

Most helpful comment

Is there any update on this request?

All 18 comments

TypeHandlers currently doesn't work with enums. They are getting ignored: https://github.com/StackExchange/Dapper/issues/259.

If you even pass the parameter using ToString method, you won't be able to select it back. Dapper won't map string to an enum property.

Dapper won't map string to an enum property.

I could be wrong, but I thought that worked just fine. But indeed the other direction (enum in c# stored as a string in to DB) is not currently supported.

Thanks @mgravell !
Do you think that this feature could be implemented someday?

Is there any update on this request?

Also very interested in this
+1

Has there been any further discussion on this?

+1 please..

Didn't this use to work? I was storing (and reading back) enums as strings in 1.50.2.

+1

please,i need this feature.

i also need this feature to seamlessly use postgres enums

This works everyone. There are tests for it
https://github.com/StackExchange/Dapper/blob/cd751c21f67348a1327d1073b716f99563d44322/Dapper.Tests/EnumTests.cs#L17-L24

In order to Save it as a string do a ToString() on the enum value.

The feature request is so that we don't have to do "ToString()" in our code. It would be nicer if dapper did it under the hood. For example,

var invoice = new Invoice() 
{
    Customer = "Bob Smith",
    Priority = InvoicePriority.Urgent
};
// What I have to do now ...
var newInvoiceId = await connection.QuerySingleAsync<int>(@"
                INSERT INTO Invoices;
                     VALUES
                           (
                            @Customer
                           ,@Priority
                           );
                SELECT CAST(SCOPE_IDENTITY() as int)",
                           new {
                           Customer = newInvoice.Customer,
                           // Have to ToString() otherwise, I end up with numbers in db, which is annoying to look
                           // at when querying the db directly and will break app if someone ever changes the
                           // order of the enums or adds/removes enums in code.
                           Priority = Priority.ToString()
                           });

// What I would like to do ...
var newInvoiceId = await connection.QuerySingleAsync<int>(@"
                INSERT INTO Invoices;
                     VALUES
                           (
                            @Customer
                           ,@Priority
                           );
                SELECT CAST(SCOPE_IDENTITY() as int)",
                           // Would be nice if the enums were stored as their "ToString"ed version 
                           newInvoice
                           );

If you're going to make an option to save the string instead of the number please implement it as an option, not an always on feature. Some shops prefer to use the underlying number instead of the string value. Maybe an attribute decorator or something, or in Dapper's settings?

+1
Any update about this matter?

For a work around I just created a Stored Procedure Model to map with the SP.

Here are my Models:

image

image

image

Then The Repo:

image

Is there a Dapper Contrib type of option available to provide this functionality? What exactly stands in the way of making this work?

Hey guys, for what its worth I am using DynamicParameters with a SPROC and I do the following:

var personType = PersonType.ABC; // PersonType is an enum dynParams.Add("PersonType", personType, DbType.String, ParameterDirection.Input, size: 50);

I set a breakpoint and inspected dynParams and I found the param value was "ABC" as a string. I havent checked the call to the sproc yet so it appears to be working?

2.0.30

Was this page helpful?
0 / 5 - 0 ratings

Related issues

unipeg picture unipeg  路  3Comments

nhathongly picture nhathongly  路  3Comments

PeterWone picture PeterWone  路  5Comments

R2D221 picture R2D221  路  4Comments

silkfire picture silkfire  路  4Comments