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

Store Enum Value as String #813

Open
lommez opened this issue Jul 4, 2017 · 28 comments · May be fixed by #1762
Open

Store Enum Value as String #813

lommez opened this issue Jul 4, 2017 · 28 comments · May be fixed by #1762

Comments

@lommez
Copy link

lommez commented Jul 4, 2017

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?

@ydogus
Copy link

ydogus commented Jul 4, 2017

TypeHandlers currently doesn't work with enums. They are getting ignored: #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.

@mgravell
Copy link
Member

mgravell commented Jul 5, 2017

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.

@lommez
Copy link
Author

lommez commented Jul 6, 2017

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

@vikram0602
Copy link

Is there any update on this request?

@Wintermute79
Copy link

Also very interested in this
+1

@philbo87
Copy link

Has there been any further discussion on this?

@cyclst
Copy link

cyclst commented Jan 4, 2018

+1 please..

@finsterdexter
Copy link

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

@chaitanyagurrapu
Copy link

+1

@toosean
Copy link

toosean commented Jul 7, 2018

please,i need this feature.

@janetr1023
Copy link

i also need this feature to seamlessly use postgres enums

@rhubley
Copy link

rhubley commented Jul 10, 2018

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.

@chaitanyagurrapu
Copy link

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

@Matt-Nelson
Copy link

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?

@hdinizribeiro
Copy link

+1
Any update about this matter?

@kbdavis07
Copy link

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

@jboarman
Copy link

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

@VictorioBerra
Copy link

VictorioBerra commented Feb 6, 2020

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

@SeppPenner
Copy link

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

@bfriesen bfriesen linked a pull request Mar 20, 2022 that will close this issue
@wael101
Copy link

wael101 commented Mar 20, 2022

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

@nyan-cat
Copy link

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!

@darkguy2008
Copy link

darkguy2008 commented Jun 1, 2023

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.

@djimmo
Copy link

djimmo commented Aug 24, 2024

Maybe this helps. I have combined some of the things I found online.

https://gist.github.com/djimmo/b54a1643f15bd4e54303992ddce4a968

@JadaVonRuth
Copy link

JadaVonRuth commented Sep 30, 2024

@djimmo I tried you code but unfortunately (with plain dapper only, no extensions) it still writes number to database during insert or update, reading enums is fine with latest version even without type handler. Is there something I am missing? It seems to be completely ignored.

@JadaVonRuth
Copy link

It's been 8 years and nothing has been done to date, what a shame. I certainly can't use .ToString() on a model coming from the data layer libraries, which I can't touch. So do I have to clone those models and replace the properties with a string each time, or construct some nonsense with dynamic parameters by hand to store it properly? That's just ridiculous. That's a serious flaw. Until then, I will continue to use the entity framework.

@djimmo
Copy link

djimmo commented Oct 7, 2024

@djimmo I tried you code but unfortunately (with plain dapper only, no extensions) it still writes number to database during insert or update, reading enums is fine with latest version even without type handler. Is there something I am missing? It seems to be completely ignored.

Which database are you using? In the example we use Oracle. If you annotate your Enum as in the example, does it work?

@JadaVonRuth
Copy link

@djimmo I am using MS SQL, it did not worked for me there so I wrote a wrapper for dapper methods that replaces original params object with one generated by reflection that replaces enum properties as strings to make it work, this way I do not have to write extra pointless DTOs or anonymous objects.

@djimmo
Copy link

djimmo commented Oct 8, 2024

@JadaVonRuth Any code snippets you could share?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.