c# – How to name foreign key in Entity framework

Question:

public class Lancamento {

     // Colunas e métodos omitidos 

     [Column("IdContaBancaria")]
     public virtual ContaBancaria ContaBancaria { get; set; }
}

public class ContaBancaria
{
        [Key] // PK
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // Será auto incremento
        [Column("idContaBancaria"]
        public int IdContaBancaria { get; set; }   

        public virtual ICollection<Lancamento> LancamentosCB { get; set; }
       // Colunas e métodos omitidos 
}

I would like to make the name of the fk explicit but the name always generated [ContaBancaria_IdContaBancaria]

How to set foreign key name?

Answer:

Using the ForeignKey attribute:

[ForeignKey("fk_nome")]
Scroll to Top