How to define queries inside EF models using keyless entity types?

Viewed 243

I just stumbled over Entity Framework Core Feature called Keyless Entity Types. The listed main usage scenarios mention:

Mapping to queries defined in the model.

I tried to find out what they mean using my favorite search engine, but couldn't find anything related. Can someone give me a hint or link the related documentation on how to define query-mapping inside EF Core models?

Thank you very much in advance!

1 Answers

There's not an example of that in the doc, but it would look like this:

        modelBuilder.Entity<BlogPostsCount>(
            eb=>
            {
                eb.HasNoKey();
                eb.ToSqlQuery("select Name, count(*) PostCount from BlogPosts group by Name");
                eb.Property(v => v.BlogName).HasColumnName("Name");
            });
Related