Mapping with Entity Framework

Asked

Viewed 48 times

1

Good afternoon,

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

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

I understand what you do in practice but not theoretically, refers to the object but explicitly what is this parameter saying? Go through the objects and see which ones have the Idestudante parameter and say that the primary key is this?

Hugs.

1 answer

1


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

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

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

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

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

Browser other questions tagged

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