Entity Framework Core Find and Composite Key

c# entity-framework-core

Question

The Find() method takes an array of objects describing the primary key you're attempting to find. The documentation is unclear as to how to handle composite primary keys. I tried searching the github repository, but was unable to find the source code for the Finder.Find() method.

For example, I've used the fluent API to define the following composite primary key:

modelBuilder.Entity<Article>()
    .HasKey( x => new { x.CommunityID, x.ArticleID } );

Do I call Find() like this:

Find( new object[] {1, 2} );

or like this:

Find( new object[] { new {CommunityID = 1, ArticleID = 2} } );

If it's the first approach, is the order of the parameters the same as the order of properties defined on the fluent API anonymous object?

1
8
12/23/2017 2:19:40 AM

Accepted Answer

Find has a signature of: params object[] keyValues

No need to pass in an array, just each key separately:

.Find(1, 2);

If it's the first approach, is the order of the parameters the same as the order of properties defined on the fluent API anonymous object?

Yes, you'll need to pass in the PKs in the same order as defined within your fluent API map.

18
12/23/2017 2:34:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow