.NET Core command-line (CLI) tool to generate Entity Framework Core model from an existing database.
- Entity Framework Core database first model generation
- Safe regeneration via region replacement
- Safe Renaming via mapping file parsing
- Optionally generate read, create and update models from entity
- Optionally generate validation and object mapper classes
Entity Framework Core Generator documentation is available via Read the Docs
More information about packaging a dotnet tool : https://www.meziantou.net/2018/06/11/how-to-publish-a-dotnet-global-tool-with-dotnet-core-2-1
After bug fixing/adding change request to the EntityFrameworkCore.Generator tool, make sure you bump the version number. Modify the properties of the EntityFrameworkCore.Generator project + modify version.props.
If the version number is updated, you can run the following command in the console (workpath = solution folder):
dotnet pack EntityFrameworkCore.Generator.sln --configuration Release
Follow the steps shown in https://dev.azure.com///_packaging?_a=feed&feed= + click on "connect to feed"
Example:
nuget.exe push -ConfigFile nuget.config -Source "sourcename" -ApiKey az EntityFrameworkCore.Generator.1.4.0.nupkg
To install EntityFrameworkCore.Generator tool, run the following command in the console
dotnet tool install --global EntityFrameworkCore.Generator
If you receive an error saying
Unable to load the service index for source https://.../nuget/v3/index.json
Response status code does not indicate success: 401 (Unauthorized)
You can try the steps explained here: https://marcstan.net/blog/2018/09/22/Global-tools-in-.Net-Core/
Or, you can download the NuGet package and try (see also dotnet/sdk#13429)
dotnet tool install --global EntityFrameworkCore.Generator --add-source C:\<folder where .nupkg file is stored> --version x.y.0
Example:
dotnet tool install --global EntityFrameworkCore.Generator --add-source C:\Projects
If you still receive the 401 response status, try to remove the package source in Visual Studio under Tools > Nuget Package Manager > Package Manager Settings > Package Sources.
This bug should be fixed in .NET Core SDK 3.0 : https://github.com/dotnet/cli/issues/10701
Verify correct installation via
dotnet tool list --global
After the tool has been installed, the efg6
command line will be available. Run efg6 --help
for command line options
To update EntityFrameworkCore.Generator tool, you can run the following command in the console (workpath doesn't matter):
dotnet tool update --global EntityFrameworkCore.Generator
Or, you can download the NuGet package and try
dotnet tool update --global EntityFrameworkCore.Generator --add-source C:\<folder where .nupkg file is stored>
Example:
dotnet tool update --global EntityFrameworkCore.Generator --add-source C:\Projects
To uninstall EntityFrameworkCore.Generator tool, run the following command in the console
dotnet tool uninstall --global EntityFrameworkCore.Generator
Entity Framework Core Generator (efg6) creates source code files from a database schema. To generate the files with no configuration, run the following
efg6 generate -c <ConnectionString>
Replace <ConnectionString>
with a valid database connection string.
The generate
command will create the follow files and directory structure by default. The root directory defaults to the current working directory. Most of the output names and locations can be customized in the configuration file
The EntityFramework DbContext file will be created in the root directory.
The entities directory will contain the generated source file for entity class representing each table.
The mapping directory contains a fluent mapping class to map each entity to its table.
The initialize
command is used to create the configuration yaml file and optionally set the connection string. The configuration file has many options to configure the generated output. See the configuration file documentation for more details.
The following command will create an initial generation.yaml
configuration file as well as setting a user secret to store the connection string.
efg6 initialize -c <ConnectionString>
When a generation.yaml
configuration file exists, you can run efg6 generate
in the same directory to generate the source using that configuration file.
Entity Framework Core Generator supports safe regeneration via region replacement and source code parsing. A typical workflow for a project requires many database changes and updates. Being able to regenerate the entities and associated files is a huge time saver.
All the templates output a region on first generation. On future regeneration, only the regions are replaced. This keeps any other changes you've made to the source file.
Example of a generated entity class
public partial class Status
{
public Status()
{
#region Generated Constructor
Tasks = new HashSet<Task>();
#endregion
}
#region Generated Properties
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int DisplayOrder { get; set; }
public bool IsActive { get; set; }
public DateTimeOffset Created { get; set; }
public string CreatedBy { get; set; }
public DateTimeOffset Updated { get; set; }
public string UpdatedBy { get; set; }
public Byte[] RowVersion { get; set; }
#endregion
#region Generated Relationships
public virtual ICollection<Task> Tasks { get; set; }
#endregion
}
When the generate
command is re-run, the Generated Constructor
, Generated Properties
and Generated Relationships
regions will be replace with the current output of the template. Any other changes outside those regions will be safe.
In order to capture and preserve Entity, Property and DbContext renames, the generate
command parses any existing mapping and DbContext class to capture how things are named. This allows you to use the full extend of Visual Studio's refactoring tools to rename things as you like. Then, when regenerating, those changes won't be lost.
Entity Framework Core Generator supports the following databases.
- SQL Server
- PostgreSQL
- MySQL
- Sqlite
The provider can be set via command line or via the configuration file.
Set via command line
efg6 generate -c <ConnectionString> -p <Provider>
Set in configuration file
database:
connectionString: 'Data Source=(local);Initial Catalog=Tracker;Integrated Security=True'
provider: SqlServer
The database schema is loaded from the metadata model factory implementation of IDatabaseModelFactory
. Entity Framework Core Generator uses the implemented interface from each of the supported providers similar to how ef dbcontext scaffold
works.
Entity Framework Core Generator supports generating Read, Create and Update view models from an entity. Many projects rely on view models to shape data. The model templates can be used to quickly get the basic view models created. The model templates also support regeneration so any database change can easily be sync'd to the view models.
- add support for navigation property renames
- update generated query methods to support CancellationToken
- add support for temporal tables
- add regex clean support for table and column names
- add support for nullable reference types
- upgrade projects to .net 6
- Breaking change to generated mapping constants
TableName
is nowTable.Name
TableSchema
is nowTable.Schema
Column{Name}
is nowColumns.{Name}
- add additional automapper for read model to update model
- Add
Table.Name
andTable.Schema
variable support in yaml configuration. - Add entity name option to control the name of the entity. Leave blank to use previous generate logic. Set under the entity -> name yaml settings.
- Add exclude table support. Set under the database -> exclude yaml settings.
- Include and Exclude expression can now be
exact
orregex
. When exact, will be direct string match. When regex, will use regular expression to match. Default is regex for legacy support.
- Add external script template support
- Misc bug fixes
- Add support for Entity Framework Core 3.0
- Add provider support for PostgreSQL, MySQL and Sqlite
- Add View support
- Add alias to commands, can use
efg6 gen
forefg6 generate
andefg6 init
forefg6 initialize
- Fix bug where base class for Entity was placed in wrong location
- Fix misc sorting issue that caused needless source control changes