-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Model Building: Reset precision for inherited model properties #31718
Comments
Duplicate of #27970. |
@ajcvickers Unfortunately, the linked issue does not explain anything. However, to me it looks like it aims at a different goal. Note that in my case here the base model is only an abstract class, so what I'm aiming at is simply the possibility to override on configuration level. It seems inconsequential to me that one can override the precision with a different value without any problems, and only cannot reset it to the special value "nothing". |
If there the base class is not mapped, then there is no configuration for it, and hence there is nothing to override. |
The base class defines a property with a PrecisionAttribute. EF Core will use the precision from this base class in the derived class to create models/migrations. Now, if I want to have this precision in 9 derived classes, but do not want to have it another derived class, I cannot reset the precision to nothing. |
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
Problem
Currently, one can set the precision of DateTime types in entity models by using the PrecisionAttribute or via FluentApi by HasPrecision().
However, if you use the attribute on the property of a base class, there is no way to unset it again in one of the derived classes.
If e.g. the base class uses
[Precision(0)]
, this will translate to the type "datetime2(0)" in SQL Server. When there is no attribute, the type "datetime2" would be used.In a derived model, using the original type "datetime2" is now not possible anymore.
Using
will still use
datetime2(0)
andprecision: 0
in Migrations.Specifying the full (original) precision
will use
datetime2(7)
in Migrations.This is mostly a problem when applying changes to an existing configuration, as then this would imply a column type change from datetime2 to some datetime2(p) type in the Migration also for the models where one does not desire this.
(This can of course be worked around by using "new" keyword for the properties or by not using the base class.)
Suggested solution
It would desirable to have an option to simply "unset" the precision again, i.e. to recreate the original situation.
This could be implemented by providing an overload for .HasPrecision() without arguments.
One would then be able to override the inherited value via FluentAPI on the derived and get
datetime2
without explicit precision again:The same should apply conceptionally for other types that use precision/scale.
The text was updated successfully, but these errors were encountered: