Git Product home page Git Product logo

clearbluedesign / entityframeworkcore.scaffolder Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 48 KB

Entity Framework Core 2.1+ extension for the Microsoft `Scaffold-DbContext` database-first scaffolder that allows you to enable lazy-loading, pluralization, and override base classes for the generated DbContext and entities.

License: MIT License

C# 100.00%
entityframeworkcore database-first scaffolder

entityframeworkcore.scaffolder's Introduction

ClearBlueDesign.EntityFrameworkCore.Scaffolder

This is an Entity Framework Core 2.1+ extension for the Microsoft Scaffold-DbContext database-first scaffolder that allows you to:

  • Use lazy loading for all related entities (virtual keyword)
  • Use EFCore.Pluralizer entity name pluralizer by Brice Lambson
  • Change the base class of the generated MyDbContext class (as may be needed when integrating with the IdentityServer)
public partial class MyDbContext 
	: IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
   ...
}
  • Import additional assemblies as part of the generated MyDbContext class (e.g. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;)
  • Override base classes and interfaces of any of the generated entities (for integration with libraries like IdentityServer and implementation of IAuditableEntity-style interfaces)
public partial class User : IdentityUser<int>, IAuditableEntity
{
   ...
}
public partial class Vehicle : IAuditableEntity
{
   ...
}

Usage

  1. Add the following package to your startup project:
Install-Package ClearBlueDesign.EntityFrameworkCore.Scaffolder -Pre
  1. Add scaffolder.json file at the root of the project and configure custom scaffolding options as needed.
  2. Add DesignTimeServices.cs file containing DesignTimeServices class that implements IDesignTimeServices interface (so that the ClearBlueDesign.EntityFrameworkCore.Scaffolder is injected into Scaffold-DbContext pipeline):
public class DesignTimeServices : IDesignTimeServices {
	public void ConfigureDesignTimeServices(IServiceCollection services) {
		services.AddScaffolder();
	}
}
  1. Run scaffolding command with the desired base scaffolder parameters:
Scaffold-DbContext 
	"<CONNECTION-STRING>" 
	"Microsoft.EntityFrameworkCore.SqlServer" 
	-Context "MyDbContext" 
	-ContextDir "Data" 
	-OutputDir "Data\Entities" 
	-Tables "Product","ProductNote","ProductHistory" 

Custom Scaffolding Options (scaffolder.json)

This scaffolder extension supports all of the official Scaffold-DbContext options (see here).

All of the new scaffolder options should be specified inside the scaffolder.json file at the root of the project:

{
	"Scaffolding": {
		"UsePluralizer": true,
		"UseDataAnnotations": false
	},

	"DbContext": {
		"Base": "DbContext"
	},

	"EntityType": {
		"UseLazyLoading": true,
		"BaseMappings": {
			"<TableName1>": [ "MyBaseClass1", "IContract" ],
			"<TableName2>": [ "MyGenericClass1<Int32>", "IContract" ]
			"<TableName3>": [ "IContract" ]
		},
		"PropertyMappings": {
			"<EntityTypeA>": {
				"<ColumnNameA>": "<PropertyNameA>",
				"<ColumnNameB>": "<PropertyNameB>",
			},
			"<EntityTypeB>": {
				"<ColumnNameC>": "<PropertyNameC>",
				"<ColumnNameD>": "<PropertyNameD>",
			}
		}
		"LoadAssemblies": [
			"My.Awesome.Project"
		]
	}
}

Roadmap

  • Add support for entity-level data validations using partial entity classes with validation attributes ([MetadataObject(typeof(Vehicle.Metadata)], [AssertThat], [RequiredIf], etc), IValidatableObject, and ValidateEntity override on the generated MyDbContext.
  • Add support for scaffolding of stored procedures into the generated MyDbContext using query types.

Contribution

Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on our guidelines for contributing and then check out one of our issues in the hotlist: community-help.

License

This project is licensed under the MIT license.

entityframeworkcore.scaffolder's People

Contributors

d3vtoolsmith avatar lambidu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

hur1can3

entityframeworkcore.scaffolder's Issues

Entities that inherits from generic types loses their type params

When specifying generic type, with type parameters, as base class for entity, for example:

"BaseMappings": {
    "Users": [ "IdentityUser<Int32>" ],
}

Generated class loses type parameters for base class,
and becomes User : IdentityUser instead of User : IdentityUser<Int32>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.