Dot-Net
如何刪除實體框架 4.1 生成的外鍵名稱中的下劃線
我有一個使用 Entity Framework 4.1 的關係,我的外鍵是自動生成的,外鍵的名稱有下劃線:
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; } }這生成的外鍵名為:Setor表中的Secretaria_Id
我想刪除這個下劃線:SecretariaId
有沒有辦法做到這一點?我更喜歡使用 DataAnnotations。
在 Fluent API 中,您可以為 FK 列命名:
modelBuilder.Entity<Setor>() .HasOptional(s => s.Secretaria) .WithMany() .Map(a => a.MapKey("SecretariaId"));我認為這對於 DataAnnotations 是不可能的。或者,您可以將外鍵公開到您的模型類中,如下所示:
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; } }約定會自動將其辨識為 FK,列名將是屬性的名稱,即
SecretariaId.