-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Using SmartEnum with EF #13
Comments
In my opinion, it doesn't make sense to have a default constructor. Enumerations aren't meant to be created on the fly. The static instances should be used (possibly by using the |
Can you share some code, either here or in a gist? |
I was following the @ardalis article here: https://ardalis.com/persisting-the-type-safe-enum-pattern-with-ef-6
So what I decided to do was copy the SmartEnum class from this repo instead of using the NuGet package and add a default constructor as in this gist: Then when I add this to my DbContext class's
Now, I haven't tried using this at runtime yet - this was just to get past the compile errors. Are there other issues I am going to have when I try to run this? |
So I am running into issues now. I want my entity class to have a property as such: That maps to an existing column in the database called But the
How do I fix this so I can still use this SmartEnum with EF? |
The error was being thrown from the Unity resolver for some reason. After double-checking all my code and doing a clean build, I got past that error. And for the most part, my SmartEnum class with the default constructor was working fine. But then in another section of the application, they are doing dynamic LINQ to Entities and I was getting an error trying to do anything with the SmartEnum except reference its value, since that's all the Thanks for listening. I'll close this issue (and if there is a way to delete an issue, you are welcome to do that.) |
@desertrambler Personally in cases like this I'd be using a lookup table in the database. Although, as Steve's article shows, you can map a complex type etc, with LINQ to Entities (L2E) you will have problems in code that uses lambdas, such as filtering or comparing etc. To alleviate your problem, you could simply declare a local variable in any method that's using L2E where you want to use a SmartEnum value, assigning them the SmartEnum's Value. Then use the variable in the code in place of the SmartEnum. This Won't Work
This Would Work
Yes, it's a little bit of a pain, but that's just a "quirk" of L2E. If you're not supplying something that L2E can understand (ie "an entity's property"), then the value that you're using to filter with has to be a primitive type. HTH |
Thanks for the tip, @yannduran! I will take a look at using this trick in the future! |
I tried to add a private default constructor to the class I derived from this
SmartEnum
class and VS2017 is telling me that thisSmartEnum
does not implement a default constructor. Do I have to "roll my own"SmartEnum
class in order to use it with EF6 by adding a default constructor in it since this doesn't have one?The text was updated successfully, but these errors were encountered: