Skip to content
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

Closed
adschmu opened this issue Sep 13, 2023 · 5 comments
Closed

Model Building: Reset precision for inherited model properties #31718

adschmu opened this issue Sep 13, 2023 · 5 comments

Comments

@adschmu
Copy link

adschmu commented Sep 13, 2023

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

            entity.Property(x => x.CreationDate)
                .HasColumnType("datetime2");

will still use datetime2(0) and precision: 0 in Migrations.

Specifying the full (original) precision

            entity.Property(x => x.LastUpdateDate)
                .HasPrecision(7)
                .HasColumnType("datetime2");

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:

            entity.Property(x => x.LastUpdateDate)
                .HasPrecision();

The same should apply conceptionally for other types that use precision/scale.

@ajcvickers
Copy link
Contributor

Duplicate of #27970.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2023
@adschmu
Copy link
Author

adschmu commented Sep 21, 2023

@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".

@ajcvickers
Copy link
Contributor

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.

If there the base class is not mapped, then there is no configuration for it, and hence there is nothing to override.

@adschmu
Copy link
Author

adschmu commented Sep 21, 2023

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.

@ajcvickers
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants