Home > Back-end >  Entity Framework Core exception renaming column
Entity Framework Core exception renaming column

Time:04-05

I have a .Net6 WinForms application using Entity Framework Core 6.0.3 and I am trying to read a simple table from a SQL Server database. I need to rename the column so that it is different in the model than what it is called in the database.

Normally (in EF6, I would add a [Column()] attribute on the property with the new name. However, when I do that it throws an exception reading the data 'Invalid column name' for whatever the new name is.

I have also tried using the modelBuilder and calling the HasColumnName() but get the same error. If I remove the attribute/model builder reference, then no exception occurs, except that I am stuck with the old column name.

[Table("RefTable1")
public partial class SpecialReferenceTable
{
   public Int32 Id { get; set;}
   [MaxLength(300)]
   [Column("NewRefColumn"]   // Throws exception Column does not exist: NewRefColumn
   public String? OldRefColumn {get;set;}
}

Is there something I am missing with renaming column in Core?

CodePudding user response:

[Column("Column name")] is used for the name of the column in the database table and the name of the property you have is the one you want to change.

  • Related