Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Store Enum Value as String

Open lommez opened this issue 7 years ago • 28 comments

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?

lommez avatar Jul 04 '17 20:07 lommez

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.

ydogus avatar Jul 04 '17 23:07 ydogus

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.

mgravell avatar Jul 05 '17 06:07 mgravell

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

lommez avatar Jul 06 '17 18:07 lommez

Is there any update on this request?

vikram0602 avatar Aug 18 '17 16:08 vikram0602

Also very interested in this +1

Wintermute79 avatar Oct 12 '17 12:10 Wintermute79

Has there been any further discussion on this?

philbo87 avatar Dec 27 '17 21:12 philbo87

+1 please..

cyclst avatar Jan 04 '18 13:01 cyclst

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

finsterdexter avatar Jan 18 '18 23:01 finsterdexter

+1

chaitanyagurrapu avatar Feb 26 '18 06:02 chaitanyagurrapu

please,i need this feature.

toosean avatar Jul 07 '18 16:07 toosean

i also need this feature to seamlessly use postgres enums

janetr1023 avatar Jul 09 '18 21:07 janetr1023

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.

rhubley avatar Jul 10 '18 18:07 rhubley

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
                           );

chaitanyagurrapu avatar Jul 11 '18 00:07 chaitanyagurrapu

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?

Matt-Nelson avatar Sep 10 '18 20:09 Matt-Nelson

+1 Any update about this matter?

hdinizribeiro avatar Feb 01 '19 10:02 hdinizribeiro

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

kbdavis07 avatar Feb 17 '19 07:02 kbdavis07

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

jboarman avatar Apr 12 '19 22:04 jboarman

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

VictorioBerra avatar Feb 06 '20 17:02 VictorioBerra

@mgravell I would like to contribute here if it's okay. Can you give me a hint where to look at concretely?

SeppPenner avatar Nov 08 '21 08:11 SeppPenner

I've run into this and I need a solution please!

wael101 avatar Mar 20 '22 12:03 wael101

That's 3rd or 4th open Dapper enum issue opened for years I found when googled for a solution, and nobody seems to be care of. What a shame!

nyan-cat avatar Dec 28 '22 12:12 nyan-cat

Man... mid-2023 already and we STILL don't have this feature. I'm so annoyed by the fact that there's people pushing to use Dapper instead of EF but these kind of decisions or low support on features like these really make it a deal-breaker, might as well end up using ADO at this pace.

darkguy2008 avatar Jun 01 '23 05:06 darkguy2008