concept fluent API in category entity framework
appears as: Fluent API, Fluent API, The Fluent API

This is an excerpt from Manning's book Entity Framework Core in Action.
Fluent API —EF Core has a method called OnModelCreating
that’s run when the EF context is first used. You can override this method and add commands, known as the Fluent API, to provide extra information to EF Core in its modeling stage. The Fluent API is the most comprehensive form of configuration information, and some features are available only via the Fluent API.
Figure 7.8 A one-to-many relationship, in which the foreign key must be in the dependent entity; in this case, the
Review
entity class. You can see in the Fluent API on the right that theBook
has a collection navigational property,Reviews
, linked to theReview
entity classes, butReview
doesn’t have a navigational property back toBook
.![]()
The Fluent API is the most comprehensive way to configure relationships, and some features, such as setting the action on deletion of the dependent entity, are available only via the Fluent API.
Listing 8.4 Registering your static method representing your UDF using Fluent API
protected override void #1 OnModelCreating(ModelBuilder modelBuilder) { //… other configuration removed for clarity modelBuilder.HasDbFunction( #2 () => MyUdfMethods.AverageVotes(default(int))) #3 .HasSchema("dbo"); #4 } #1 Fluent API is placed inside the OnModelCreating method inside your application’s DbContext. #2 HasDbFunction will register your static method as the way to access your UDF. #3 Adds a call to your static method representation of your UDF code. The method isn’t called, but the lambda function is read to find out the name, return type, and parameters of the method. #4 You can add options. Here you add HasSchema, as EF Core 2.0 had a problem of not setting the default schema property (fixed in 2.1). Other options, such as HasName, set the name of the UDF in the database.