.net - 如何删除 Entity Framework 4.1 生成的外键名称中的下划线

标签 .net entity-framework entity-framework-4.1 data-annotations

我有一个使用 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/

相关文章:

css - .Net bundler 在遇到加号时会去除空格?

c# - System.Threading.Timer 与 System.Threading.Thread.Sleep 解析 - .NET Timer 不使用系统时钟解析

c# - MVC3 + Entity Framework ,更新实体中的选择性字段

c# - EF + 通用存储库 + 加载相关实体 : only explicit load works

c# - 使用 LINQ 搜索树

.net - ./在我使用 OpenFileDialog 时更改目标

c# - Entity Framework - 选择不同的

c# - 使用 Entity SQL 但没有 FROM 子句调用 UDF?

c# - 如何配置 EF Code First 以不映射特定类型?

asp.net-mvc-3 - 更新对象图时 Entity Framework 的断开行为