我有一个使用 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; }
}
此生成的外键名为: Secretaria_Id 表中 setter
我想删除这个下划线:秘书处编号
有没有办法做到这一点?我更喜欢使用 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
.
关于.net - 如何删除 Entity Framework 4.1 生成的外键名称中的下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749095/