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

SQL Server Migrations: Rebuild columns after altering HasComputedColumnSql #5789

Closed
maksir opened this issue Jun 16, 2016 · 2 comments
Closed
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

@maksir
Copy link

maksir commented Jun 16, 2016

Steps to reproduce

I have a class

public class User : IdentityUser<int>
{
    public string Name { get; set; }
    public string DisplayName { get; set; }
}

and DbContext

AppDbContext : IdentityDbContext<User, Role, int>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.Entity<User>().Property(p => p.DisplayName).HasComputedColumnSql("[Name]");
    }
}

execute

dotnet ef migrations add User_DisplayName
dotnet ef database update
All done.

Modify OnModelCreating() like this:

builder.Entity<User>().Property(p => p.DisplayName)
    .HasComputedColumnSql("[Name] + ' (' + [Email] + ')'");

execute again

dotnet ef migrations add User_DisplayName_fix
dotnet ef database update

The issue

Error Number:156,State:1,Class:15
Incorrect syntax near the keyword 'AS'.'

Further technical details

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final",
Operating system: Win 10
Visual Studio version: 2015
MS SQL 2012

Cause of the error

In this file src/Microsoft.EntityFrameworkCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs
lines number 60 and 400 generating script:

alter table User alter column DisplayName as [Name] + ' (' + [Email] + ')'

This is wrong.
T-SQL does not support argument 'AS' for command 'Alter column'.

For computed column, need 2 commands.

  1. drop computed column
  2. create new computed column with same name
@rowanmiller
Copy link
Contributor

If you are changing a column from non-computed to computed you are also dropping all the existing data from the column. Perhaps our model differ should reason about this and generate a DropColumnOperation and AddColumnOperation rather than the alter in this case.

@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@bricelam bricelam changed the title SqlServer migration script generation error on 'alter table xxx alter column yyy' Migrations: Rebuild columns after adding HasComputedColumnSql Aug 12, 2016
@bricelam bricelam changed the title Migrations: Rebuild columns after adding HasComputedColumnSql SQL Server Migrations: Rebuild columns after altering HasComputedColumnSql Aug 12, 2016
@bricelam
Copy link
Contributor

If you are changing a column from non-computed to computed...

This is not what the code above is doing. It just changes the computed expression--the column was previously computed too.

As far as I can tell, Oracle supports altering computed columns just like any other column. I think we should treat this issue as specific to SQL Server and rebuild the column in its Migrations SQL generator.

@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 16, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.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