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

Cannot alter column which is a part of composite primary key in EF Core 3.0 #19048

Closed
TanvirArjel opened this issue Nov 24, 2019 · 1 comment
Closed
Assignees

Comments

@TanvirArjel
Copy link

TanvirArjel commented Nov 24, 2019

I already have a table in database for the the following entity:

public class EmployeeAddress 
{
    public long EmployeeId { get; set; }
    public string AddressType { get; set; }
    public string AddressText { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public string PostCode { get; set; }
    public string AdditionalInfo { get; set; }
}

Where EmployeeId and AddressType are the composite primary key.

Now I have added MaxLength attribute on AddressType as follows:'

public class EmployeeAddressConfiguration : IEntityTypeConfiguration<EmployeeAddress>
{
    public void Configure(EntityTypeBuilder<EmployeeAddress> builder)
    {
        builder.ToTable("EmployeeAddresses");
        builder.HasKey(ea => new { ea.EmployeeId, ea.AddressType });

        builder.Property(ea => ea.AddressType).HasMaxLength(10).IsRequired();
        ......
    }
}

And Generated migration as follows:

migrationBuilder.AlterColumn<string>(
            name: "AddressType",
            table: "EmployeeAddresses",
            maxLength: 10,
            nullable: false,
            oldClrType: typeof(string),
            oldType: "nvarchar(450)");

Now after applying update-database command, getting the following error:

The object 'PK_EmployeeAddresses' is dependent on column 'AddressType'.
ALTER TABLE ALTER COLUMN AddressType failed because one or more objects access this column.

It seems that I cannot alter AddressType column without removing this from the composite primary key. Actually EF Core should have dropped the composite primary and recreated if any of the columns of the composite primary is altered.

Further technical details

EF Core version: 3.0
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3.10)

@bricelam
Copy link
Contributor

Duplicate of #16758

@bricelam bricelam marked this as a duplicate of #16758 May 29, 2020
@bricelam bricelam removed this from the 5.0.0 milestone May 29, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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

3 participants