2015-02-24 57 views
1

這一直困擾我很長一段時間,但當我的實體框架試圖執行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 。有人有任何想法嗎?

+0

什麼錯誤消息以及何時發生? – Colin 2015-02-24 17:07:28

+0

@Colin它給出了錯誤/Extent1/./Bpid_Id/無效的標識符 – Rex 2015-02-24 17:24:39

回答

2

Bpid_id由EF創建,因爲它不能自動確定關係。嘗試添加註釋:

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

這實際上指出我在正確的方向。謝謝!!! – Rex 2015-02-24 19:39:26

1

您已在Projectship

public virtual Bpid Bpid { get; set; }

您還沒有指定一個外鍵去與導航屬性,因此實體框架選擇了一個指定的導航屬性,並選擇了名稱Bpid_Id。它應該在數據庫中。它不應該是「不存在」。

你也許會發現它更容易使用實體框架,如果你添加一個外鍵是這樣的:

public int BpidId { get; set; }

參考文獻:

Why does Entity Framework Reinsert Existing Objects into My Database?

Making Do with Absent Foreign Keys

+0

你指出我正確的方向!我現在覺得自己已經解決了這個問題:( – Rex 2015-02-24 19:40:56