Available Presets

For Entity Framework Core v2.1, there are two presets available, SD.EntityFramework.Core v2.1, which is used for .NET Full and SD.EntityFramework.Core v2.1 Netstandard, which generates code for .NET Standard 2.0. Both will generate two Visual Studio projects, each in their own folder, with a csproj file. For .NET core you have to use .NETStandard 2.0 or higher.

The presets generate 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.

.NET Core

To generate code for .NET Core, please select Netstandard2.0 as target platform when generating code. This will generate a csproj file compatible with the .NET core tooling in Visual Studio 2017 15.3+.

Extending the generated DbContext to supply the connection string

As Entity Framework Core is also suitable to run on .NET core, it doesn't have a built-in way to read the connection string from the application's config file. To be able to read the connection string from the application's config file, please do the following:

  • Create a new partial class of the generated ProjectNameDataContext file in the RootNamespace.Persistence project folder, and call it ProjectNameDataContext_Extended.cs. If you're using Entity Framework Core on .NET full, add this file to the csproj file too within Visual Studio.
  • 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
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
            base.OnConfiguring(optionsBuilder);
        }
    }
}

If you're using .NET Core, you can also use one of the other mechanisms described in the Microsoft Entity Framework Core documentation.

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.