Mapping the model to multiple tables with EntityFramework.Core

Xiaodi Yan
6 min readMar 27, 2020

With EntityFramework Core, we can use attributes or Fluent API to config the model mappings. One day I just got a scenario that needs a new mapping style. There is a system that generates lots of data every day and it needs to store the data per table for each day. For example, the database looks like:

All the tables have the same structure. So how could we change the mapping to avoid creating lots of models?

In this article, I will show you how to change the mapping to handle this situation. You can also use this method to extend more usages.

Creating the .NET Core 3.1 project

Now we can use .NET Core 3.1 which is an LTS version of .NET Core. So that you can easily upgrade it to .NET 5 in the future.

I suppose you already have the latest .NET Core SDK installed on your machine. If not, you can download it from https://dotnet.microsoft.com/download. Then you can use dotnet CLI to create projects. For this sample, I will use .NET Core 3.1.

Let create a new .NET Core Console project named DynamicModelDemo.

dotnet new console --name DynamicModelDemo

We can create a solution by using this command:

dotnet new sln --name DynamicModelDemo

Then add the project to the solution:

dotnet sln add "DynamicModelDemo/DynamicModelDemo.csproj"

--

--

Xiaodi Yan

Microsoft MVP (.NET & AI) / MCT / .NET, Azure, AI Developer / Learner