C
C#11mo ago
BekirK

✅ How to set enum default int value

Hi everyone, I have an enum type. For example as below. Normally, the value of enums in the db starts from 0. I know that if we want to start from 1, saying '=1' for the first value is enough. The question I want to ask you about is that you can do this process automatically with typeconfiguration etc. Can I do it somewhere? The reason I want this is because I have many enums. And I want them all to start from 1. What do you think about it :)
public enum MyEnum
{
FValue,
SValue,
TValue
}
public enum MyEnum
{
FValue,
SValue,
TValue
}
22 Replies
Pobiega
Pobiega11mo ago
No, you can't do it automatically.
BekirK
BekirKOP11mo ago
Thats very sharp answer 😅 I thought I could do it :/
Pobiega
Pobiega11mo ago
¯\_(ツ)_/¯ Well, you can't. Maybe with some IL weaving or source gen a quick google search for a fody plugin reveals nothing, so if you want to go the IL weaving route you'll need to write one yourself
BekirK
BekirKOP11mo ago
Hmm i think this is gonna be complicated. It doesnt afford this effort
Pobiega
Pobiega11mo ago
agreed. not sure why you want them all to start with 1 anyways
BekirK
BekirKOP11mo ago
This is a team decision. We did not find it appropriate for types to start with 0 in the db. We want all enums to start from 1. But we didn't want to always define for 1 in enum
Pobiega
Pobiega11mo ago
Might be able to create a value converter If it's specifically between database and code.. but it might get confusing Seeing the value 1 in dB, but inte casting the enum gives 0 :p How about using strings for enum values in database? Thats what we do at work
BekirK
BekirKOP11mo ago
Normally we keep it that way. But since data kept as string reduces performance in indexed queries, we now must refactor all enums as int. We were warned about this by our db admin ://
Pobiega
Pobiega11mo ago
Thats a micro-optimization and if that is your bottleneck you have other issues with your query structures.
BekirK
BekirKOP11mo ago
It's actually not a bottleneck for us. But it is just a necessary step for optimization. Although it makes sense to keep the enum as a string in the db for reporting etc., the correct thing is to keep it numerical. So our team decided to choose this.
Pobiega
Pobiega11mo ago
as said, its a micro-optimization it will save less then 1% of query time
BekirK
BekirKOP11mo ago
Yep you re right actually
Pobiega
Pobiega11mo ago
I find it really weird that your team takes decisions without taking core language concerns into account thou like, "ok we should have numeric enums" is fine. but "and they should start at 1" is weird, in a 0-based enum language
BekirK
BekirKOP11mo ago
Its not just our desicion. Db admin suggest this to us. We dont want to do this really. Because there is a lot of work/effort/hours for this. because you should preserve the previous values of each enum according to their counterparts in the db. There are many things you need to pay attention to when going to the production environment, etc.
Pobiega
Pobiega11mo ago
To actually be helpful thou: if you are fine with it being 0-based in C#, and its only in the database you want it to be 1-based, a custom value converter will be the way to go
BekirK
BekirKOP11mo ago
This is actually not a necessity. We just whink about it
Pobiega
Pobiega11mo ago
I think you should tell your db admin and/or boss about this the dev time is a cost, and the fragility is a hazard ie, if you add an enum to an entity class, you MUST remember to go in and add the custom value converter in the type config or you get 0-based index
BekirK
BekirKOP11mo ago
hmm i see
Pobiega
Pobiega11mo ago
it wont throw any errors if you don't, it will just be 0-based you could write an analyzer for it, I guess but it will be a fairly complicated 🙂
BekirK
BekirKOP11mo ago
Ahahha exactly, I already gave up on it after talking to you Thanks a lot for your help and advice. I learned good things
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX11mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server