How to remove underscore in foreign key names created by Entity Framework 4.1

I have a relationship using Entity Framework 4.1, and my foreign key is generated automatically, the foreign key names are underlined:

public class Setor : Entity { public long Id { get; set; } public string Nome { get; set; } public virtual Secretaria Secretaria { get; set; } } public class Secretaria : Entity { public long Id { get; set; } public string Nome { get; set; } } 

This generated foreign key with the name: Secretaria_Id in the Setor table

I want to remove this underline: SecretariaId

Is there any way to do this? I prefer to use DataAnnotations.

+7
source share
1 answer

In the Fluent API, you can name the FK columns:

 modelBuilder.Entity<Setor>() .HasOptional(s => s.Secretaria) .WithMany() .Map(a => a.MapKey("SecretariaId")); 

I think this is not possible in DataAnnotations. In addition, you can open the foreign key in your model class, for example:

 public class Setor : Entity { public long Id { get; set; } public string Nome { get; set; } public long? SecretariaId { get; set; } public virtual Secretaria Secretaria { get; set; } } 

Conventions will recognize this automatically, since FK and the column name will be the name of the property, i.e. SecretariaId .

+9
source

All Articles