How to migrate from Entity Framework EDMX files to fluent API syntax Part 1

Anton Dambrouski
4 min readAug 22, 2023

--

Hey reader, if you found this article then probably you have a similar task as I did on the project. The reason why I wanted to migrate to fluent API syntax was that it would allow me to migrate projects to .Net Core much easier in the future.

To doing so I’m going to write a custom ‘migrator’ because I didn’t find any solutions that would do the migration. I saw several examples of creating a database from EDMX file and then applying the database-first approach to generate code from the newly created database. You can take a look here: Model for an Existing Database. In fact it didn’t work for me, because I had the database-first approach in the project, so the newly created database from an EDMX file was slitly different from the existing one.

So, my goal is to give you a quick overview of EDMX file structure and build a custom migrator. Links:

What’s the EDMX file?

The EDMX is an entity data model file in XML format which defines mappings between database tables and entities in our code. It has three main parts:

  1. CSDL (Conceptual Schema definition language) — defines the entities structure of the application
  2. SSDL (Storage schema definition language) — defines table structure of the database
  3. MSL ( Mapping Schema language ) — defines mappings between tables and entities

It is the minimal knowledge you need to understand what I’m going to write about. I guess the only missing part is examples, but you’ll see them in the implementing section. Let’s do it!

Create a database and generate an EDMX file

Firstly, let’s take a look at database schema because as I mentioned before I had a database-first approach. As you can see from the diagram below we have all types of relations to handle which are one-to-one, one-to-many and many-to-many. Click here to learn more about relationships in SQL:

Let’s generate the EDMX file from this database:

As you can see EF generator adds comments with sections, so let’s get to coding!

Migrator creation

Microsoft has already defined classes for working with the EDMX file. You can find it in the System.Data.Entity.Core.Metadata.Emd and System.Data.Entity.Core.Mapping namespaces. The most important classes for me are

  • EdmItemCollection — parses and stores info about entities which is part of CSDL;
  • StoreItemCollection — parses and stores info about tables which is part of SSDL;
  • StorageMappingItemCollection — parses and builds relations between entities of the app and tables of the database which is MSL.

First of all I want to split the process into two main parts: parse the EDMX file and create files with FluentApi syntax.

I want to create processors where one of them will be EdmxFileProcessor and the second one will be FluentApiProcessor. Let’s define a common interface:

public interface IProcessor
{
void Process(ProcessorContext context);
}

Add appsettings.json file:

{
"edmxFilePath": "Path to Edmx File\\AppModel.edmx"
}

Install packages for configuration:

dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json

And build our pipeline of processor:

using Microsoft.Extensions.Configuration;
using FluentApiMigrator.Interfaces;
using FluentApiMigrator.Models;
using FluentApiMigrator.Processors;


var logger = NLog.LogManager.GetCurrentClassLogger();
try
{
var configs = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();

var context = new ProcessorContext()
{
EdmxFilePath = configs.GetSection("edmxFilePath").Value,
};

var processors = new List<IProcessor> { new EdmxFileProcessor(), new FluentApiProcessor() };
foreach (var processor in processors)
{
processor.Process(context);
}
}
catch (Exception ex)
{
logger.Error(ex, "Error occurred during migration proccess");
}
finally
{
NLog.LogManager.Shutdown();
}

There is also context file, which we can use across different processors.

public class ProcessorContext
{
public string EdmxFilePath { get; set; }
}

The final structure of the application:

I think it’s enough for this article. We’ve covered the basics of the EDMX file, it’s sections and the structure of the project I want to build. In the next article, I will show you how to parse the EDMX file and use parsed information to create FluentApi syntax.

--

--

Responses (1)