实体框架在查询中创建不存在的列

时间:2015-02-24 15:11:35

标签: c# oracle entity-framework ef-code-first

这让我困惑了很长一段时间但是当我的实体框架试图执行oracle查询时,我一直收到无效的标识符错误。有问题的课程如下:

public class Projectship : ModelTrackingBase
{

    [Column("PROJECTID")]     
    public long ProjectId { get; set; }

    [Column("VISITID")]
    public long VisitId { get; set; } 

    public virtual Bpid Bpid { get; set; } //new
    public virtual Visit Visit { get; set; }

}

 public class Bpid : EntityIdBase
{
    [Column("BUDPRJID")]
    [MaxLength(38)]
    public string BPId { get; set; }

    [Column("DESCRIPTION")]
    [MaxLength(255)]
    public string Description { get; set; }

    [Column("CSTOBJ")]
    [MaxLength(255)]
    public string Custobj { get; set; }

    [Column("PVID")]
    [MaxLength(255)]
    public string Pvid { get; set; }
    public virtual ICollection<Projectship> Projectships { get; set; }  

    public IEnumerable<Visit> Visits
    {
        get { return Projectships.Select(p => p.Visit); }
    }

    [NotMapped]
    public string DisplayName
    {
        get { return string.Format("{0}: {1}", BPId , Description); }
    }

}

现在EntityIdBase具有以下内容:

public class EntityIdBase : EntityBase
    {
        [Column("ID")]
        public long Id { get; set; }
    }

它尝试继续在查询中查找列Bpid_Id。有人有任何想法吗?

2 个答案:

答案 0 :(得分:2)

Bpid_id由EF创建,因为它无法自动确定关系。尝试添加注释:

[ForeignKey("ID")]
public virtual Bpid Bpid { get; set; } //new

答案 1 :(得分:2)

您已在Projectship

中指定了导航属性

public virtual Bpid Bpid { get; set; }

您尚未指定使用导航属性的外键,因此Entity Framework已选择一个,并且已选择名称Bpid_Id。它应该在数据库中。它不应该是#34;不存在&#34;。

如果你添加这样的外键,你可能会发现使用Entity Framework更容易:

public int BpidId { get; set; }

参考文献:

Why does Entity Framework Reinsert Existing Objects into My Database?

Making Do with Absent Foreign Keys