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

.NET 5.0 RC1 p.SetColumnType("[money]") results in wrong type in migration #22569

Closed
henrikdahl8240 opened this issue Sep 16, 2020 · 6 comments
Closed
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@henrikdahl8240
Copy link

henrikdahl8240 commented Sep 16, 2020

If you have a property of type decimal and do p.SetColumnType("[money]") using .NET 5.0 RC1 the migration will specify another type, because it will specify [money](18,2).

Money however neither has precision nor scale, so it fails when updating the database.

If you manually substitute "[money](18,2)" => "[money]" as you actually specified using p.SetColumnType("[money]"), it works fine.

@henrikdahl8240 henrikdahl8240 changed the title .NET 5.0 RC1 p.SetColumnType("[money]") results in errors in migration .NET 5.0 RC1 p.SetColumnType("[money]") results in wrong type in migration Sep 16, 2020
@ajcvickers
Copy link
Contributor

@henrikdahl8240 Using square brackets around type names is not something that EF Core expects, and hence EF doesn't recognize [money] as a type it knows and falls back to the default behavior for a decimal property with an unknown store type. It shouldn't add (18, 2) to this, so that's a bug. However, when using the SQL Server type in a form that EF understands (p.SetColumnType("money")) I see the correct behavior.

Is there a reason you're specifying type names in this way?

@henrikdahl8240
Copy link
Author

@ajcvickers OK, so I will remove the brackets. I thought that the argument is forwarded as is to SQL Server and if you try to use the context menu in SQL Server Management Studio for a database and select Tasks => Generate Scripts ..., types appear with brackets around in line with the usual reasons for using brackets when using SQL Server. Therefore I put the brackets around, like e.g. [money].
In the previews it has worked fine. In my opinion it was quite reasonable to expect, that the argument for SetColumnType should be specified in terms of the software dealing with columns, i.e. in terms of SQL Server. The summary for SetColumnType is "Sets the database type of the column to which the property is mapped." and I suppose that what is given in a script produced by SQL Server Management Studio should be the canonical representation, which I therefore used.

@ajcvickers
Copy link
Contributor

@henrikdahl8240 We'll discuss.

@ajcvickers ajcvickers self-assigned this Sep 18, 2020
@ajcvickers ajcvickers added this to the 5.0.0 milestone Sep 18, 2020
@henrikdahl8240
Copy link
Author

I think there should be made a clear decision on the nature/syntax of the actual argument and the documentation should be clear, so you have no doubt as you use the feature.

@ajcvickers
Copy link
Contributor

@henrikdahl8240 That's reasonable, although in my 12+ years on EF I think this is the first time I have ever seen anyone use square brackets in this API.

@henrikdahl8240
Copy link
Author

I also have this construction in my code:
modelBuilder.Model.GetEntityTypes().SelectMany(entityType => entityType.GetDeclaredProperties()).Where(p => p.ClrType == typeof(DateTimeOffset)).ToList().ForEach(p => p.SetColumnType("[datetimeoffset](0)"));

As far as I remember, it does not work withoug brackets, but only with brackets, at least in some of the .NET 5.0 previews and therefore I have consistently put brackets around the type lexemes.

ajcvickers added a commit that referenced this issue Sep 18, 2020
Fixes #22569

In EF Core 3.1, it was possible to specify a SQL Server column type using square brackets. For example, "[money]" instead of "money". EF Core didn't understand this, so we treated it as an unknown decimal type, and passed it through to SQL Server as-is.

In EF Core 5.0, we added support for precision and scale specified independently of the type. We don't do this when we know the type is `money` because doing so is not valid. However, since we don't recognize "[money]" it gets precision and scale added.

The fix is to recognize SQL Server types with square brackets. Note that only type names without spaces can have square brackets.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 19, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-rc2 Sep 22, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-rc2, 5.0.0 Nov 7, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants