Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Entity Framework Core 6 how to use Find() method with part of the composite key?

I see that Find method in EF requires the arguments to be in the same order as in the HasKey method.

However, what if I have a composite key let’s say ColA ColB ColC. But I only want to find the entities where ColA=5 and ColC=6 – without using LINQ, how would I do it in the most efficient (using Find()) way?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

what if I have a composite key let’s say ColA ColB ColC. But I only want to find the entities where ColA=5 and ColC=6

Then you don’t want to do what the Find() method is documented to do:

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given primary key values

You don’t have all the values that make up the primary key (i.e. pointing to a single record), so you want to search: just build a Where() query instead.

without using LINQ

That is an unnecessary complication.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading