Problem Returning Items in a Relationship in the Entity Framework

4

Speak Personal All right!?

I'm going through the following problem in the entity framework that I think is some error I'm having at the time of relating two classes:

I have a class called: RequestPathPathIndexPath that is related to a class called Item, as shown below, I first made the relationship between them directly in the class, but since it was not working, I tried to do by Fluent API as below, I am able to save in the database of data normally but when I go to make an inquiry some items return normally but others do not. What can it be? I'm thinking it has to do with the fact that the relationship is with a string field and not a code, can that be it?

Below an image with the example of what is happening, in the same list some objects load and others do not, before they ask me all they exist in the bank and if I consult later they return ... it can be something with index or something like that?

publicclassSolicitacaoGarantiaItemPecas{[Key]publicintSolicitacaoGarantiaItemPecasID{get;set;}publicstringCodigoItem{get;set;}[ForeignKey("CodigoItem")]
    public virtual Item ItemPeca { get; set; }

    public decimal Quantidade { get; set; }
    public int CodPedido { get; set; }
    public string SituacaoPedido { get; set; }

    public int SolicitacaoGarantiaItemID { get; set; }
    [ForeignKey("SolicitacaoGarantiaItemID")]
    public virtual SolicitacaoGarantiaItem SolicitacaoGarantiaItem { get; set; }
}

Fluent API:

modelBuilder.Entity<SolicitacaoGarantiaItemPecas>().HasRequired(a => a.ItemPeca).WithMany().HasForeignKey(c => c.CodigoItem);

Thank you! Alex

    
asked by anonymous 04.08.2016 / 02:54

1 answer

2

I was able to solve this problem apparently we have to specify a maximum size of the fields when they are PK / FKs looks to me strange but as I am working with Entity Framework a short time I do not know if this is normal or not, but it was like I managed to solve it, see below:

    Classe Item
    [Column(TypeName = "VARCHAR")]
    [StringLength(25)]
    [Key]
    public string CodigoItem { get; set; }

    Classe SolicitacaoitemPecas
    [Column(TypeName = "VARCHAR")]
    [StringLength(25)]
    public string CodigoItem { get; set; }
    [ForeignKey("CodigoItem")]
    public virtual Item ItemPeca { get; set; }

Doing so the whole thing came back to work ...

    
04.08.2016 / 21:52