Available Presets

For Entity Framework Core v3, there is one preset available, SD.EntityFramework.Core v3 Netstandard, which generates code for .NET Standard 2.1. This preset will generate two Visual Studio projects, each in their own folder, with a csproj file.

The preset generates all entity classes, value type classes and typed list row classes in separate files and add proper XML comments to the generated code. It furthermore generates a DbContext class called ProjectNameDataContext.

There are two Visual Studio projects generated: one called RootNamespace.Model, in the folder Model, which contains all the domain classes (entity classes and typed list classes) and one called RootNamespace.Persistence, in the folder Persistence, which contains the DbContext derived class and the ModelBuilder class which defines the mappings.

Compiling and using the generated code

After you've generated code, please follow the following guide to use the generated code in your own solution

Compiling and referencing

To use both generated Visual Studio projects in your own solution, be sure that the RootNamespace.Persistence Visual Studio project references the RootNamespace.Model Visual Studio project.

In the RootNamespace.Persistence Visual Studio project you have to add a reference to the Entity Framework Core nuget package called Microsoft.EntityFrameworkCore. Additionally you have to add a reference to the database specific nuget package for the database type the code is mapped to, e.g. Microsoft.EntityFrameworkCore.SqlServer.

If you're using spatial types in your project, you have to make sure you reference the right spatial type package. Please consult the Entity Framework Core spatial types documentation page for further details.

Extending the generated DbContext to supply the connection string

As it's possible to supply the connection string in various ways, depending on your application type/setup, the generated code doesn't have a built-in way to read the connection string from the application's config file. The generated code does have a constructor which accepts a DbContextOptions instance, which is configured in e.g. your asp.net startup. If you use that route, you don't need to add anything.

In case of when you want to read the connection string from a config file, you have to follow the steps below. The net effect is that you can either use a config file or pass the connection string to the constructor of the generated DbContext class:

  • Create a new partial class of the generated ProjectNameDataContext file in the RootNamespace.Persistence project folder, and call it ProjectNameDataContext_Extended.cs.
  • Add the following code to the created partial class file, where you change RootNamespace with the RootNamespace of your project, ProjectName with the ProjectName of your own project, and ConnectionStringName with the name of the connection string in the generated app.config file in the RootNamespace.Persistence project folder. If you're using another database than SqlServer, please change the method UseSqlServer with the method required for that database.
using System;
using System.Configuration;
using Microsoft.EntityFrameworkCore;

namespace RootNamespace
{
    public partial class ProjectNameDataContext
    {
        private string _connectionString;

        public ProjectNameDataContext(string connectionString) : base()
        {
            _connectionString = connectionString;
        }
        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer((string.IsNullOrEmpty(_connectionString) ?
                                            ConfigurationManager.ConnectionStrings["ConnectionStringName"]?.ConnectionString 
                                            : _connectionString) ?? string.Empty );
            base.OnConfiguring(optionsBuilder);
        }
    }
}

You can also use one of the other mechanisms described in the Microsoft Entity Framework Core documentation.

If you're using spatial types, be sure to use the NetTopologySuite extension method with the database specific extension method, see the Entity Framework Core documentation for details.

Extending the ModelBuilder code

The generated mappings are defined in the ProjectNameModelBuilder class. It defines per entity a method which contains all the mappings of the entity. If you want to extend this mapping manually, please define a derived class of ProjectNameModelBuilder and override the mapping method of the entity you want to extend the mapping information of.

Then create a derived class of the ProjectNameDataContext class and override the method OnModelCreating(). In that method you do:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    new YourDerivedModelBuilderClass().BuildModel(modelBuilder);
    OnModelCreatingComplete();
}

where YourDerivedModelBuilderClass is the class derived from the ProjectNameModelBuilder.

OnModelCreatingComplete

Additionally, at the end of the OnModelCreating method a partial method, OnModelCreatingComplete is called. By default this partial method has no implementation, but if you want to append additional modeling information, you can by implementing this partial method in a partial class of the generated context class.