2016-12-31 172 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

不客气,很高兴它帮助:)新年快乐! –