Entity Framework Core Relationships with Fluent API by Henrique Siebert Domareski Medium

In Entity Framework 6, the DbModelBuilder class acts as a Fluent API using which we can configure many different things. It provides more options of configurations than Data Annotation attributes. In the below table we have listed down some of the most common methods of Entity Framework Core Fluent API.

In a Fluent API, we can link together different functionalities to get a particular result. We see this used many times in Entity Framework Core and when manipulating List type of items using lambda expressions. In a Fluent API, we can link together different functionality to get a particular result. Besides the brute enforcement of a protocol, a fluent API also integrates neatly with certain IDE services. Auto-completion is a common IDE feature that suggests possible code continuations. When applied to a fluent API, auto-completion actively guides the programmer through the API, by showing what calls may continue the chain, while ignoring methods that would break it.

If application tries to insert/update record with NULL values Entity Framework throws DbEntityValidationException. By default Entity Framework will create Primary Key for property having name as IDor + ID. For this tutorial, we will use data models created using EF Core Code First Migration with separate assemblyThis creates two different domain classes which will be configured using r-lib devtools: Tools to make an R developer’s life easier in this tutorial. In this blogpost explains how to configure entities using Fluent API of Entity Framework Core or EF6 above versions for the Code First approach. This will give you details about how to configure business rules with Fluent API methods for PrimaryKey, Unique, Index, Required, Identity, etc. Are available for relation fields when using the model’s create or update query.

Add new related records to an existing record

Override the OnModelCreating method and use a parameter modelBuilder of type ModelBuilder to configure domain classes, as shown below. Explains how to configure entities using Fluent API of Entity Framework Core or EF6 above versions for the Code First approach. Now in the Dbcontext class, it’s necessary to override the method OnModelCreating. Inside this method, we are going to set the configuration for the entities using Fluent API.

fluent api

The more the use of the API has that language like flow, the more fluent it is. We can now model these stages as interfaces now, and use the transition between these stages to determine the return type of each methods. There are multiple ways regarding the process about designing a fluent API, for example this article shows a great process on it, by defining various grammar rules and then using them to create interfaces.

Filter on presence of related records

Registers a type as a complex type in the model and returns an object that can be used to configure the complex type. This method can be called multiple times for the same type to perform multiple lines of configuration. It allows you to configure default database schema to be used for tables.

fluent api

That way, we not only concentrate the API logic in a single class, but we also avoid multiple object instantiations in a single chain. This alternative design still gives the benefits of a fluent API, assuming the master class is not visible to the user. A relationship, in the context of databases, is a situation that exists between two relational database tables, when one table has a foreign key that references the primary key of the other table. When working with Code First, you define your model by defining your domain CLR classes. By default, the Entity Framework uses the Code First conventions to map your classes to the database schema.

This code centric approach to building an Entity Data Model is known as Code First. Migration is a way to update the database in an incrementally way. So always when you create a new entity class in the application, or when you change some existing entity, you need to generate a new migration and update the database with this migration.


A line item can be skippable, meaning I’d prefer to deliver without this line item rather than delay the whole order. You can always share your thoughts on this by leaving a comment on this post. If you liked what you read, you can try reading some other posts on my blog as well, and you can also connect with me on my socials.

  • Entity Framework Fluent API is used to configure domain classes to override conventions.
  • These conventions are the rules to create the model, based in the entities classes.
  • We can do similar things in with a non-fluent API as well, and granted do it as well, but like in the LINQ example, it would need a lot more lines of code and a bunch of temporary variables.
  • You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.

You can also see the setter method FromImage that adds content to the object and returns it back, which will enable us to use “method-chaining” on our objects. Here I am using StringBuilder to store the actual string content of the dockerfile and the setters are essentially just adding content to this string builder. As you can see, my constructor is private which initializes a new object and I have a factory method that returns a new object. A introductory post talking about fluent apis, its pros and cons and how to design one for yourself from scratch.

You can have a one entity class that maps to multiple tables from the same database . For example, Product data model will have details for Product as well as it’s inventory details. However at database level Products and Product inventory details needs to be in separate tables. Since I wrote this post https://cryptominer.services/ this term’s been used rather widely, which gives me a nice feeling of tingly gratification. I’ve refined my ideas about fluent interfaces and internal DSLs in the book I’ve been working on. I’ve also noticed a common misconception – many people seem to equate fluent interfaces with Method Chaining.

For One-To-Many relationship, it’s possible to configure the relationship using both ends of the relationship. On the example below, I implemented in both ways to show as an example. Following code sets Maximum length for ProductName column as 50 characters.

Connect or create a record

In this case, Entity Framework will add additional WHERE Clause for column ProductName. So when User2 tries to update same product it’s query becomes as shown below which will return zero records because User1 already committed ProductName as “Product 2”. When columns must have values you can mark those columns as NOT NULL. Entity Framework code first should configure such properties with the IsRequired()method.

The navigation property for this class is an IEnumerable of Student because a course can have one or more students, so this needs to be a list. The following code will create two different tables using Code First. You can explicitly mention the DataType of database column irrespective of CLR property types. If your database column name needs to be different than the property name of the Data Model you can use the HasColumnName to configure column name.

In Raku, there are many approaches, but one of the simplest is to declare attributes as read/write and use the given keyword. The type annotations are optional, but the native gradual typing makes it much safer to write directly to public attributes. The following table lists important methods for each type of configuration. As we already mentioned the three parts of an API — object, options, and result — we will start with the order that the “Order” interface will represent. A highlight is that this interface has some interfaces, which will be responsible for demonstrating our options. In practice, creating a fluent API means developing an API in which it is unnecessary to memorize the next steps or methods, allowing for a natural and continuous sequence as if it were a menu of options.

Solicitar orçamento

Informações do produto