更新实体以首先在 EF 代码中公开现有外键 属性?

Update Entity to expose existing foreign key property in EF code first?

我试过这个问题:How to expose Foreign Key property to existing entity having navigational property using EF6 Code First 但它不起作用。我收到以下错误:

The index 'IX_FormEntry_Id' is dependent on column 'FormEntry_Id'.
ALTER TABLE ALTER COLUMN FormEntry_Id failed because one or more objects 
access this column.

我只是想在 FormReport POCO 上公开 FormEntryId:

public class FormReport : Entity
{
    public Guid? FormEntryId { get; set; } //I added this
    public virtual FormEntry FormEntry { get; set; }
    //other props
}

我使用了上述链接答案中概述的映射:

public class FormReportMapping : EntityTypeConfiguration<FormReport>
{
    public FormReportMapping()
    {
        HasRequired(x => x.FormEntry)
        .WithOptional()
        .Map(p => p.MapKey("FormEntry_Id"));

        new EntityMap().MapInheritedProperties(this);
    }
}

我希望它能识别,嘿,它就是这样,不需要改变,但事实并非如此,我该怎么做?

编辑:我想保留我的命名约定,它与 EF 自动生成的命名约定不匹配。我的 FK 属性中没有一个在我的 POCO 中使用下划线。但这就是数据库中的列名。

用数据标注很容易做到:

public class FormReport : Entity
{
    [Column("FormEntry_Id")]) // Map to the existing column name
    [ForeignKey("FormEntry")] // Associate with the navigation property 
    public Guid? FormEntryId { get; set; }
    public virtual FormEntry FormEntry { get; set; }
    //other props
}

流利的怎么样API,看来只有效仿上面的方法才能达到目的:

public class FormReportMapping : EntityTypeConfiguration<FormReport>
{
    public FormReportMapping()
    {
        Property(x => x.FormEntryId)
            .HasColumnName("FormEntry_Id")
            .HasColumnAnnotation("ForeignKey", "FormEntry");
        // ...
    }
}