Entity framework creating a non existent column in the query

落爺英雄遲暮 提交于 2019-12-11 02:35:17

问题


This has been puzzling me for quite some time but I keep getting an invalid identifier error when my entity framework tries to execute an oracle query. The classes in question are the following:

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; }

}

and

 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); }
    }

}

Now the EntityIdBase has the following:

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

It tries to keep on looking for a column Bpid_Id in the query. Does someone have any idea?


回答1:


Bpid_id is created by EF because it can't automatically determine the relationship. Try adding the annotation:

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



回答2:


You have specified a navigation property in the Projectship class

public virtual Bpid Bpid { get; set; }

You have not specified a foreign key to go with the navigation property so Entity Framework has chosen one, and it has chosen the name Bpid_Id. And it should be in the database. It should not be "non-existent".

You will probably find it easier to use Entity Framework if you add a foreign key like this:

public int BpidId { get; set; }

References:

Why does Entity Framework Reinsert Existing Objects into My Database?

Making Do with Absent Foreign Keys



来源:https://stackoverflow.com/questions/28699357/entity-framework-creating-a-non-existent-column-in-the-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!