✅ 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 :)
22 Replies
No, you can't do it automatically.
Thats very sharp answer 😅
I thought I could do it :/
¯\_(ツ)_/¯
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
Hmm i think this is gonna be complicated. It doesnt afford this effort
agreed.
not sure why you want them all to start with 1 anyways
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
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
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 ://
Thats a micro-optimization and if that is your bottleneck you have other issues with your query structures.
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.
as said, its a micro-optimization
it will save less then 1% of query time
Yep you re right actually
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
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.
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
This is actually not a necessity. We just whink about it
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
hmm i see
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 🙂
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•12mo ago
Message Not Public
Sign In & Join Server To View
Use the /close command to mark a forum thread as answered