-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
use DefaultBoolean, DefaultInt and similar for distinguishing between nullable primitive type and non-nullable with default DB constraint #33729
Comments
Why not just use a nullable type (e.g. |
Because If there are multiple columns where some are Or same situation with |
In EF LINQ queries, you should be able to always access It's true that once you've loaded the entity and are outside of a LINQ query, you now have a Beyond that, IMHO introducing a whole new type just to make that distinction apparent when coding is a bit sledgehammer that isn't really justified. It would also mean users start integrating an EF-specific "defaultable" type on their CLR types, which goes against the plain-.NET POCO philosophy of EF, and which many/most users wouldn't appreciate. So while I understand (and agree) with what you're saying in theory, I don't think it makes sense for EF to introduce a special defaultable type and plumb support for it all through its internals. If .NET already had a standard |
Well yes if you use DB-first approach with default settings then you have to look into the database or look into DbContext OnModelCreating (for every property for every model you have) which is inconvenient and prone to error. Imagine real world this entity and that you don't know what the properties mean (so you don't know whether they are
Yes I understand it would be pretty non-standard for EF to introduce such type without it existing in C# in general so this won't be changed anytime soon.
But at the same time I cannot think of anything why would C# in general introduce something like |
If you use database-first, I'm not sure I see EF generating a
One simple option - which you may not like - is to simply always check |
If e.g. bool or int has default value set in database, there is an issue specifying if null is to be inserted or default value should be used by the DB.
The problem is created by the nature of the C# language which has default "zero-like" value for all primitive types ("structs") which does not exactly correspond to DB ability to specify default value.
By using a new structure which will add 3rd value, the problem is solved.
For example, DevExpress uses this for UI Controls.
https://docs.devexpress.com/CoreLibraries/DevExpress.Utils.DefaultBoolean
The data type could have implicit conversion to the primitive type for output mapping purposes because the issue is only present on input mapping (C# => database) not in output mapping (database => C#).
Use something like that to be able to distinguish "no value" and "set default value" for SQL Server (and probably all other DB engines which support default values in nullable columns as well).
Real life situation:
Now I am stuck on EF Core Power tools mapping bool column with default value to bool? to allow inserting "null" from C# which triggers using default value on DB level but this creates confusion when reading data from DB because now the column is represented as bool? in C# while it's not nullable in DB.
If the behavior is disabled, I cannot then let database insert default value for me.
But I need both - ability to use default DB value AND represent the correct data type in C# when reading from DB and the DefaultBoolean with implicit conversion operator to bool (similarly for int etc.) should be solving it.
The text was updated successfully, but these errors were encountered: