Skip to content

boone34/Data.Entity.Edmx

Repository files navigation

Overview

The purpose of this project is to facilitate migrating from entity framework 6.0 to entity framework core (EFC). More specifically it provides tooling to transform EDMX files into EFC models.

It differs from current tooling that I have found which reverse engineer databases to create the model. This tooling converts the conceptual model in EDMX to the EFC model. In this way all changes made to the conceptual EDMX model are reflected in the EFC model. This includes:

  • Entity names
  • Property names
  • Added associations that do not exist in the database (especially useful for views)

This greatly facilitates upgrading large entity framework 6.0 models to EFC where a lot of effort has already gone into customizing the model.

Usage

Requirements

  1. The database must already exist. This tooling will not create an EFC model that will create a database for you.
  2. The tooling uses T4 templates in Visual Studio to create the model. It has only been tested in Visual Studio 2019.
  3. The model generated targets EFC 5.0 & EFC 6.0 which are required for how they implement many to many relationships.

Create a Model

Install the Project Template

Download the Edmx To Entity Framework Core project template and install it in Visual Studio.

I have found that this can be done by copying the Edmx To Entity Framework Core.zip file to your Documents\Visual Studio 2019\Templates\ProjectTemplates folder and restarting Visual Studio. Note: I have found that it does not work if you copy it to a sub folder such as Visual C# but your mileage may differ.

Create the Model Project

Create the model project using the Edmx To Entity Framework Core project template. Finding the template can be a little tedious using visual studio as you cannot search for it or use project type filters. Also project templates do not appear to be sorted in any particular order. So set project type filters to all and scroll until you find the project template.

Project Selector

It may appear at the top labeled as new or towards the end.

On creating the project you will get the following project structure:

Project Structure

Before generating the model you need to configure things by editing the Generator.ini file. In this file set the following parameters:

Parameter Comments
EdmxPath Path to the EDMX file. This can be relative to the EdmxToEfCore folder.
SubFolder Project sub folder to create the model files in. If not set model files will be created in the project root.
Namespace Namespace for the model classes
ContextName Name of the model DbContext class
KeysHaveDefaultValueSql This can be used to indicate that simple keys are automatically generated by means other than Identity and suppresses the use of ValueGeneratedNever() for none Identity computed keys and configures them with HasDefaultValueSql(). This can be used for databases that use sequences. See the WideWorldImporters sample for an example of this. Default value for this is false.

For an example of using this file see the WideWorldImporters sample.

Once configured generate the model by Right Clicking Generator.tt and selecting Run Custom Tool.

Run Generator

Before compiling the project add the following nuget packages (I may add these to the project template in the future):

  • Microsoft.EntityFrameworkCore.Relational
  • NetTopologySuite

Customize the DbContext

The generated model is database and feature agnostic. For this reason a customized db context needs to be created that inherits the model context and implements the database type and any desired features. The customized context is also a good place to address any functionality that is not captured by the generated model. the customized context can be implemented in the model or separate project.

For an example of this see the WideWorldImporters/Sales/Dal.SqlServer project. There the custom context implements:

  • Using Sql Server
  • Using lazy loading
  • Configures computed columns for versioned tables (information that is not in the EDMX file).
  • Adds a function to get next sequence values.

Customize the Model or Generation

All classes generated are partial so if possible try to customize things in partial classes.

Also feel free to modify the T4 files to meet your requirements. I implemented this as a project template and not a nuget package to allow for this.

Wide World Importers Sample

To run/test the sample you will need the sample database which can be found here.

The EDMX file can be found the WideWorldImporters/Sales/Edmx project. It implements a sales model which is conceptualized in various ways:

  • Tedious and redundant property names such as Color.ColorID and Color.ColorName are renamed to Color.Id and Color.Name.
  • Foreign key properties have Id appended to their names (LastEditedBy -> LastEditedById).
  • Navigation property names have been cleaned up to reflect their meaning.

The model generation project is WideWorldImporters/Sales/Dal.

The custom context project is WideWorldImporters/Sales/Dal.SqlServer. There the custom context implements:

  • Using Sql Server
  • Using lazy loading
  • Configures computed columns for versioned tables (information that is not in the EDMX file).
  • Adds a function to get next sequence values.

About

Create entity framework core model from edmx file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages