2016-12-31 175 views
1

我在使用數據註釋時缺少一些東西。數據註釋不會創建一對多的對象引用

這是我的第一類

[Table("PriceFeed")] 
public class PriceFeed : History 
{ 
    public PriceFeed() 
    { 
     this.Votes = new List<PriceVote>(); 
     this.History = new List<PriceFeed__History>(); 
    } 

    [Key] 
    public long Id { get; set; } 

    [ForeignKey("Store")] 
    public long Store_Id { get; set; } 

    [ForeignKey("Item")] 
    public long Item_Id { get; set; } 

    [Required] 
    public decimal Price { get; set; } 

    public Store Store { get; set; } 

    public Item Item { get; set; } 

    public virtual ICollection<PriceFeed__History> History { get; set; } 

} 

這是我第二類

[Table("PriceFeed__History")] 
public class PriceFeed__History : History 
{ 

    [Key] 
    public long Id { get; set; } 

    [ForeignKey("PriceFeed")] 
    public long PriceFeed_Id { get; set; } 

    [Required] 
    public decimal Price { get; set; } 

    public virtual PriceFeed PriceFeed { get; set; } 

} 

當我運行加載遷移,它正確地創建數據庫,但是當我試圖訪問PriceFeed。歷史它給我一個錯誤

{"Message":"An error has occurred.","ExceptionMessage":"A specified Include path is not valid. The EntityType 'Verdinhas.Web.Contexts.PriceFeed' does not declare a navigation property with the name 'PriceFeed__History'." 

我一直使用API​​ Fluent並由我自己鍵入E碼像

.Entity<Student>() 
       .HasRequired<Standard>(s => s.Standard) 
       .WithMany(s => s.Students) 
       .HasForeignKey(s => s.StdId); 

但現在我使用的數據說明,當我產生遷移,也不會創建「withmany」像上面。

我在做什麼錯?

+0

異常消息來自某些查詢(帶有'Include'),您沒有顯示。 –

+0

我正在使用存儲庫模式並調用包含歷史記錄表的pricerepository,就像這個PriceRepository.Get(x => x.Id == priceId,null,「Store,Item,PriceFeed__History」); –

+1

好吧,你去了 - 它應該是屬性名稱,例如「商店,物品,歷史」 –

回答

1

該問題與您的模型中似乎正確的數據註釋無關。

正如在評論中提到的,異常是由嘗試使用Includestring代碼造成了「'PriceFeed__History」 - 你似乎認爲你應該指定相關的實體類型,但實際上你需要指定導航屬性名稱,在您的情況下是「歷史」。

+0

令人驚歎的,謝謝 –

+0

不客氣,很高興它幫助:)新年快樂! –