1

I am using Entity Framework's automatic migrations with a code-first approach. I dont use the packet manager console and dont have access to coded migrations.

I have a line representing my table in my model here :

        public virtual DbSet<Customer> Customers { get; set; }

I renamed one of the fields of Customer :

[Table("Customers")]
public partial class Customer
{
    [Key]
    [Column(TypeName = "numeric")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int idCustomer { get; set; }
    public string name { get; set; }
    public int age { get; set; }
    public string mail { get; set; }
    public string password { get; set; }
}

I just changed "mail" to "mailModified". But when the database got updated, Entity deleted all data from the name field. I think it deleted my column to create a new one with the new name.

How to avoid that ? How to make him understand to only rename the column ?

Thanks for any participation

Elekk
  • 41
  • 1
  • 1
  • 6

2 Answers2

3

You have to do it using Migration commands.. so that data won't be lost ..

Else u can add the following code to auto-generated up and down method

RenameColumn("dbo.MyTable", "NewColumn", "OldColumn");
Abi
  • 283
  • 3
  • 16
1

Try using Column Attribute when you create your table class

class Table
{
    [Column("ColumnName")]
    public int Column1 { get; set; }
    [Column("ColumnName")]
    public int Column2 { get; set; }
}
Sammy
  • 48
  • 4