Mapping with Entity Framework

1

Good afternoon,

When analyzing a code I see in the mapping the following line:

this.HasKey(t => t.IdEstudante);

Understand what you do in practice but not theoretically, refers to the object but explicitly what this parameter is saying? It traverses the objects and sees which ones have the parameter StudentId and say the primary key is this?

Hugs.

    
asked by anonymous 03.01.2019 / 19:12

1 answer

1

The HasKey method has a signature that has a delegate parameter that receives its entity as a parameter and returns a object . This means that the content you return (in thesis can be anything because it is a object ) will be your primary key.

An example (example only, do not use this implementation) of what you could do to see what is accepted is to create a method that meets the signature and perform the test:

public object Teste(Entidade a)
{
    return 10;
}

builder.HasKey(a => Teste(a));

So the method has the purpose of defining what the primary key of your entity will be, so we do not define a "search criterion" as you commented above because it only needs a return than the Entity Framework will use as the primary key for this entity.

    
03.01.2019 / 19:43