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

Reverse engineering: Scaffold-DbContext can result in invalid SQL for computed column constraint #7918

Closed
pablotdv opened this issue Mar 16, 2017 · 6 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@pablotdv
Copy link

pablotdv commented Mar 16, 2017

When I run the "Scaffold-DbContext" command on an existing database. I'm having problems with the calculated columns.

The script of my Table

CREATE TABLE [dbo].[Caixas](
	[CaixaId] [uniqueidentifier] NOT NULL,
	[Percentual] [decimal](18, 2) NOT NULL,
	[Quantidade] [decimal](18, 2) NOT NULL,
	[Valor] [decimal](18, 2) NOT NULL,
	[ValorDesconto]  AS (([Valor]*[Quantidade])/([Percentual]/(100))),
 CONSTRAINT [PK_Caixas] PRIMARY KEY CLUSTERED 
(
	[CaixaId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

After running the command

Scaffold-DbContext "Server=.\SQLExpress;Database=TestComputedColumn;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2

My OnModelCreating was as follows:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Caixas>(entity =>
            {
                entity.HasKey(e => e.CaixaId)
                    .HasName("PK_Caixas");

                entity.Property(e => e.CaixaId).ValueGeneratedNever();

                entity.Property(e => e.Percentual).HasColumnType("decimal");

                entity.Property(e => e.Quantidade).HasColumnType("decimal");

                entity.Property(e => e.Valor).HasColumnType("decimal");

                entity.Property(e => e.ValorDesconto)
                    .HasColumnType("decimal")
                    .HasComputedColumnSql("[Valor]*[Quantidade])/([Percentual]/(100)")
                    .ValueGeneratedOnAddOrUpdate();
            });
        }

Note the attribute HasComputedColumnSql, where I have some missing parentheses.

The correct is HasComputedColumnSql("([Valor]*[Quantidade])/([Percentual]/(100)")

Further technical details

EF Core version: netcoreapp1.1
Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Operating system:
IDE: (e.g. Visual Studio 2017)

@ajcvickers ajcvickers added this to the 2.0.0 milestone Mar 17, 2017
@ajcvickers
Copy link
Contributor

@lajones We may have some special handling for this in EF6; you might want to look at it for reference.

@pablotdv
Copy link
Author

So I would have some tutorial explaining how I could do to debug the EF project code.

@ajcvickers
Copy link
Contributor

@pablotdv I'm not sure exactly what you are asking, but if you mean building and working with the EF code yourself, then there some instructions here: https://github.com/aspnet/EntityFramework/wiki/Getting-and-Building-the-Code

@lajones
Copy link
Contributor

lajones commented Apr 13, 2017

Consider trimming the default value at the same time - see discussion in issue 8077

@lajones
Copy link
Contributor

lajones commented Apr 19, 2017

Note: we decided not to trim. In fact we are now not interpreting the value we get from the metadata at all - we just put it into the HasComputedColumnSql() method (this also applies to the HasDefaultValueSql() method) exactly as it comes from the DB.

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@lajones
Copy link
Contributor

lajones commented Apr 20, 2017

Fixed by PR #8217.

@lajones lajones closed this as completed Apr 20, 2017
@lajones lajones added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 20, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0, 2.0.0-preview1 Apr 24, 2017
@ajcvickers ajcvickers changed the title Scaffold-DbContext Computed Column Reverse engineering: Scaffold-DbContext can result in invalid SQL for computed column constraint May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants