Available Presets
For Entity Framework Core v1, there's one preset available, SD.EntityFramework.Core v1. The preset generates code for .NET full and will generate two Visual Studio projects, each in their own folder, with a csproj file. The code is usable on .NET core too, for that you have to create a project.json file manually. LLBLGen Pro doesn't generate a project.json file as this project format is about to be surpassed by the successor of csproj. See below how to easily create the project.json file for your projects if you need one.
The preset generates all entity classes and typed list row classes in separate files and adds proper XML comments to the generated code. It furthermore generates a DbContext class called ProjectNameDataContext.
There are two VS.NET 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 VS.NET projects in your own solution, be sure that the RootNamespace.Persistence VS.NET project references the RootNamespace.Model VS.NET project.
In the RootNamespace.Persistence VS.NET 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
On .NET Core, you'll have to add project.json files (or if you're on the newer tooling for .NET core, csproj files with the newer format) to the two generated code projects and use the default wildcard system for .net core project files to include all *.cs
files. The easiest way to create a project file is by running dotnet new -t lib
on the command line in the folder of the generated project, e.g. the Model folder. This will give you two extra files: Library.cs (which you can delete) and a project file, be it either project.json or if you're using the newer .net core sdk tooling, a csproj file. Alter the project file generated by dotnet
to your liking.
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, andConnectionStringName
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 methodUseSqlServer
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);
}
where YourDerivedModelBuilderClass
is the class derived from the ProjectNameModelBuilder.