EF core changes primary key columns depending on the property name

39 Views Asked by At

I'm aware about some conventions of EF core, such as when naming a property "Id".

But this one is really strange. I'm dealing with a legacy database where I'm now at least renaming the properties, but not yet the columns themselves:

[Column("OtherEntityNr")]
public long? OtherEntityNr { get; set; }

I changed to:

[Column("OtherEntityNr")]
public long? OtherEntityId { get; set; }   // <----- replaced "Nr" with "Id"

This change lead to a migration being created that changed nullable to false. However, this is not the case, as you see the column is a long? FK.

migrationBuilder.AlterColumn<long>(
    name: "OtherEntityNr",
    table: "OtherEntity",
    type: "bigint",
    nullable: false,
    defaultValue: 0L,
    oldClrType: typeof(long),
    oldType: "bigint",
    oldNullable: true);

Question: How can I change the column name without triggering EF to create logical changes based on naming conventions?

0

There are 0 best solutions below