命名外键列

时间:2014-06-17 04:31:48

标签: entity-framework entity-framework-6

我定义了这两个表:

[Table("languages")]
public class Language
{
    [Key]
    [Column("language_id")]
    public int LCID { get; set; }
    [Column("title")]
    public string Title { get; set; }
    [Column("enabled")]
    public bool Enabled { get; set; }
    [Column("default")]
    public bool Default { get; set; }
}
[Table("localized_content")]
public class LocalizedContent
{
    [Key]
    [Column("content_id")]
    public int Id { get; set; }
    [Key]
    [Column("language_id")]
    public virtual Language Language { get; set; }
    [Column("content")]
    public string Content { get; set; }
}

在LocalizedContent类中,我希望使用列属性中定义的名称创建列,但EF忽略它,并创建名为Language_LCID的tolumn:

enter image description here

如何强制将其命名为language_id?

1 个答案:

答案 0 :(得分:1)

为此你必须定义另一个类型为Language的主键的键,它是int并将其标记为Foreign Key

[Table("localized_content")]
public class LocalizedContent
{
    [Key]
    [Column("content_id")]
    public int Id { get; set; }

    [ForeignKey("LanguageId")]
    public virtual Language Language { get; set; }

    [Key]
    [Column("language_id")]
    public int LanguageId{get; set;}
    [Column("content")]
    public string Content { get; set; }
}