-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Comments
If you even pass the parameter using |
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 ! |
Is there any update on this request? |
Also very interested in this |
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 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 |
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
I set a breakpoint and inspected 2.0.30 |
@mgravell I would like to contribute here if it's okay. Can you give me a hint where to look at concretely? |
I've run into this and I need a solution please! |
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! |
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. |
Maybe this helps. I have combined some of the things I found online. https://gist.github.com/djimmo/b54a1643f15bd4e54303992ddce4a968 |
@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. |
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. |
Which database are you using? In the example we use Oracle. If you annotate your Enum as in the example, does it work? |
@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. |
@JadaVonRuth Any code snippets you could share? |
I have a simple enum just like this:
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?
The text was updated successfully, but these errors were encountered: