Problem returning items in a Relationship in the Entity Framework

Asked

Viewed 167 times

4

Talk Personal All right!?

I’m going through the following problem in the Entity framework that I think is some mistake I’m making when it comes to relating two classes:

I have a Class called: Solicitacaogarantiaitempecas that this related to a class called Item, as figure below, first I did the relationship between them directly in the class, but as it was not working I tried to do by Fluent API as below, I am able to save in the database normally but when I go to make a query 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 field of the type string and not a code, can it be this?

Below an image with the example of what is occurring, in the same list some objects load and others do not, before you ask me all they exist in the bank and if I consult later they return... it may be something with Dice or something similar?

inserir a descrição da imagem aqui

Classes Relacionadas

    public class SolicitacaoGarantiaItemPecas
{
    [Key]
    public int SolicitacaoGarantiaItemPecasID { get; set; }

    public string CodigoItem { 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

  • Let me get this straight. The same routine, under the same conditions, sometimes returns the relationships and at other times does not return?

  • 1

    That’s right William, but yesterday and discovered the problem, in my table of items the primary key was in a text field without specifying the size, the key was only with a string field what I did I will specify a maximum size for the key and the Fks of the other classes and the query started to return all classes. [Column(Typename = "VARCHAR")] [Stringlength(25)] [Key] public string Codeitem { get; set; }

  • Giving .Include( x => x.Entidade) would not work ?

1 answer

2

I managed to solve this problem apparently we have to specify a maximum size of fields when they are PK/Fks looks seemed strange but as I am working with Entity Framework for a little while I can not say if this is normal or not, but it was how I managed to solve, 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 started working again...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.