Flexible Mapping – Example – Entity Framework Core 2.0

When we create a model, sometimes we just use properties that will represent columns in a SQL Server table. Most of the times this may be enough, but there will be other situations in which we will want to use a combination of property and field. The idea then is that we can use a field to represent a column in a SQL Server table, and we will use the property to perform a transformation on the data we are going to insert. We call this flexible mapping .

The idea of ​​flexible mapping is to be able to map not only properties with columns, but fields with columns. An advantage of doing this is that we can then add a data transformation when inserting records in our database. So, if we always want to apply a data transformation to something that we insert in a table, we can do it at the model level. So, no matter where you use the Entity Framework to perform the data insertion, this transformation will always be applied. Let’s see an example.

This example will be done with Entity Framework Core 2.0.

What we are going to do is that we are going to take a model called Student, which has a property called Name, which stores the student’s name. We are going to add a field that will work with the Name column. Then we will modify the Name property so that it is responsible for making the following transformation: Make the first letter of each word in upper case, and the rest in lowercase. Thus, if we assign the string “juAn VAldeZ” to the Name property, “Juan Valdez” will be saved in the database.

This is the Student model which has the Name property and the _Name field:

public class Student
        public int Id { get; set; }
        private string _Name;
        public string Name { get
                return _Name;
            set {
                _Name = string.Join(' ',
                value.Split(' ').Select(x => x[0].ToString().ToUpper() + x.Substring(1).ToLower()).ToArray());

        public DateTime Birthdate { get; set; }


Notice that we now have a field called _Name, which is assigned a value through the Name property. In the setter of the Name property, notice that we are using LINQ code to fulfill our mission. In short, this code takes the value that has been passed to the Name property, divides it into its different “words”, then takes the first letter of each word and capitalizes it, and then concatenates it with the rest of the word, in lowercase, then join the results in a final string, and assign the new string to the _Name field.

Now what we have to do is tell Entity Framework that we want to associate the Name column with the _Name field. For this we must use the Fluent API. If you are not familiar with it, think of the Fluent API as a part of the Context (the class that inherits from DbContext) in which we can make certain configurations to how the Entity Framework should work with our tables. The idea is that through the Fluent API we can customize some of the Entity Framework options, such as the name of the tables. For now, let’s just place the following code in the Context class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
   modelBuilder.Entity<Student>().Property(x => x.Name).HasField("_Name");

The OnModelCreating method is where we make the Fluent API configurations. As you can see, the code that we placed inside of this method tells Entity Framework that the Name property has a field called _Name. With this, Entity Framework knows that it must take the value of the _Name field to fill the Name column in the database when inserting a record. Let’s try this:

using (var context = new ApplicationDbContext())
   var student1 = new Student();
   student1.Name = "juAn VAldeZ";
   student1.Birthdate = new DateTime(1987, 2, 5);

As you can see, this code is responsible for inserting a student whose name was entered as “juAn VAldeZ”. However, if we run this code, and we go to our database, we will see the following result:

Chapter 3 - Flexible Mapping


We can use Flexible Mapping to associate columns of our SQL Server table with fields of our model. The advantage of this is that it allows us to apply transformations to our values prior to Entity Framework inserting them into the database.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s