Click here to Skip to main content
13,769,643 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

88.4K views
84 bookmarked
Posted 12 Dec 2016
Licenced CPOL

Scaffolding Entity Framework Core 2 with CatFactory

, 29 Oct 2018
Rate this:
Please Sign up or sign in to vote.
Scaffolding Entity Framework Core 2 with CatFactory

Introduction

What is CatFactory?

CatFactory it's a scaffolding engine for .NET Core built in C#.

How it Works?

The key thing in CatFactory it's import an existing database from a SQL Server instance, then scaffold code for specific technology. In some cases we can replace the database from SQL Server instance with a in memory database.

To understand the scope for CatFactory, in few words CatFactory is the core, to have more packages we can create them with this naming convention: CatFactory.PackageName.

In the sample code, the basic flow for existing database is:

  • Create Database Factory
  • Import Database
  • Create instance of Project (Entity Framework Core, Dapper, etc)
  • Build Features (One feature per schema)
  • Scaffold objects, these methods read all objects from database and create instances for code builders

Concepts Behind CatFactory

Database Type Map

One of things I don't like to get equivalent between SQL data type for CLR is use magic strings, after of review the more "fancy" way to resolve a type equivalence is to have a class that allows to know the equivalence between SQL data type and CLR type.

Using this table as reference, now CatFactory has a class with name DatabaseTypeMap. Database class contains a property with all mappings with name Mappings, so this property is filled by Import feature for SQL Server package.

Class Definition:

namespace CatFactory.Mapping
{
    public class DatabaseTypeMap
    {
        public string DatabaseType { get; set; }
        
        public bool AllowsLengthInDeclaration { get; set; }
        
        public bool AllowsPrecInDeclaration { get; set; }
        
        public bool AllowsScaleInDeclaration { get; set; }
        
        public string ClrFullNameType { get; set; }
        
        public bool HasClrFullNameType { get; }
        
        public string ClrAliasType { get; set; }
        
        public bool HasClrAliasType { get; }
        
        public bool AllowClrNullable { get; set; }
        
        public DbType DbTypeEnum { get; set; }
        
        public bool IsUserDefined { get; set; }
        
        public string ParentDatabaseType { get; set; }
        
        public string Collation { get; set; }
    }
}

Code Sample:

// Get mappings
var mappings = database.DatabaseTypeMaps;

// Resolve CLR type
var mapsForString = mappings.Where(item => item.ClrType == typeof(string)).ToList();

// Resolve SQL Server type
var mapForVarchar = mappings.FirstOrDefault(item => item.DatabaseType == "varchar");

Project Selection

A project selection is a limit to apply settings for objects match with pattern.

GlobalSelection is the default selection for project, contains a default instance of settings.

Patterns:

Pattern Scope
Sales.Order Applies for specific object with name Sales.Order
Sales.* Applies for all objects inside of Sales schema
*.Order Applies for all objects with name Order with no matter schema
*.* Applies for all objects, this is the global selection

Code Sample:

// Apply settings for Project
project.GlobalSelection(settings =>
{
    settings.ForceOverwrite = true;
    settings.AuditEntity = new AuditEntity("CreationUser", "CreationDateTime", "LastUpdateUser", "LastUpdateDateTime");
    settings.ConcurrencyToken = "Timestamp";
});

// Apply settings for specific object
project.Select("Sales.Order", settings =>
{
    settings.ForceOverwrite = true;
    settings.AuditEntity = new AuditEntity("CreationUser", "CreationDateTime", "LastUpdateUser", "LastUpdateDateTime");
    settings.ConcurrencyToken = "Timestamp";
    settings.EntitiesWithDataContracts = true;
});

Event Handlers to Scaffold

In order to provide a more flexible way in scaffolding, there are two delegates in CatFactory, one to perform an action before of scaffolding and another one to handle and action after of scaffolding.

Code Sample:

// Add event handlers to before and after of scaffold

project.ScaffoldingDefinition += (source, args) =>
{
    // Add code to perform operations with code builder instance before to create code file
};

project.ScaffoldedDefinition += (source, args) =>
{
    // Add code to perform operations after of create code file
};

Workshop

One of things I don't like about dapper is to have all definitions for queries in strings or string builders... I prefer to have an object that builds queries and in that way reduce code lines quantity but I don't know if that concept breaks Dapper philosophy, I really weant to know about that because scaffold high quality code is fundamental in CatFactory, so I don't want to add an implementation that breaks the main concept behind an ORM...

Anyway I have invested some time to research about how can I solve query building for repositories and I have no found any object that allows to build a query from object's definition, there are frameworks that provide CRUD functions but something like LINQ there isn't, as I know of course if I wrong about this point please let me know in comments.

So I'll provide a draft for query building and take your time to let me know your feedback and then please answer 2 questions:

  1. This implementation breaks Dapper concept?
  2. What do you think about to have metadata for entities in Dapper?

Query Builder Draft

Select all

var query = QueryBuilder
    .Select<Shipper>();

// Output:
// select [ShipperID], [CompanyName], [Phone] from [dbo].[Shipper]

Select by key

var query = QueryBuilder
    .Select<Shipper>()
    .Where("ShipperID", QueryOperator.Equals, 1);

// Output:
// select [ShipperID], [CompanyName], [Phone] from [dbo].[Shipper] where [ShipperID] = 1

Insert

var query = QueryBuilder
    .Insert<Shipper>(identity: "ShipperID");

// Output:
// insert into [dbo].[Shipper] ([CompanyName], [Phone]) values (@companyName, @phone)
// select @shipperID = @@identity

Update

var query = QueryBuilder
    .Update<Shipper>(key: new string[] { "ShipperID" });

// Output:
// update [dbo].[Shipper] set [CompanyName] = @companyName, [Phone] = @phone where [ShipperID] = @shipperID

Delete

var query = QueryBuilder
    .Delete<Shipper>(key: new string[] { "ShipperID" });

// Output:
// delete from [dbo].[Shipper] where [ShipperID] = @shipperID

Select by

// Search by
var query = QueryBuilder
    .Select<Shipper>()
    .Where("CompanyName", QueryOperator.Like, "%a%")
    .And("Phone", QueryOperator.Like, "%a%");

// Output:
// select [ShipperID], [CompanyName], [Phone] from [Shipper] where [CompanyName] like '%a%' and [Phone] like '%a%'

Shipper is an entity for this example, I have found the following issues with this solution:

  • There isn't information for schema (e.g. dbo, Production, Purchasing, Sales)
  • There isn't a way to know if one table with name "Order Details" is mapped to entity with Name OrderDetail

The above points can be solve if there is any information for table and entity (C# class), something like metadata, we can have an interface with name IEntity like this:

public interface IEntity
{
	Table ToTable();
}

Then create a class with name Shipper and implement interface:

public class Shipper : IEntity
{
 public int? ShipperID { get; set; }
 
 public string CompanyName { get; set; }
 
 public string Phone { get; set; }
 
 public Table ToTable()
  => new Table
  {
   Schema = "dbo",
   Name = "Shipper",
   Identity = new Identity("ShipperID", 1, 1),
   PrimaryKey = new PrimaryKey("ShipperID")
   Columns = new List<Column>
   {
    new Column
    {
     Name = "ShipperID",
     Type = "int"
    },
    new Column
    {
     Name = "CompanyName",
     Type = "varchar",
     Lenght = 50
    },
    new Column
    {
     Name = "Phone",
     Type = "varchar",
     Length = 25
    }
   }
  };
 }
}

In that way we can have all "metadata" for all entities and get that definitions to build queries in dynamic way, so we can reduce code lines in our repositories.

The definition for Table, Columns, Identity and PrimaryKey already exists in CatFactory, so we can reuse those definitions for this purpose :)

Please let me know what do you think about this implementation, make sense?

According to feedback from developers and to provide a better experience for users, I'm working on some improvements to get a more clean way to work with CatFactory:

Working with database

// Import from existing database
var database = SqlServerDatabaseFactory.Import("YourConnectionStringHere");

// Read all tables
foreach (var table in database.Tables)
{
    // Check primary key on table's definition
    if (table.PrimaryKey == null)
    {
        continue;
    }
    
    // Check identity on table's definition
    if (table.Identity != null)
    {
        var identityName = table.Identity.Name;
    }
    
    // Read all columns
    foreach (var column in table.Columns)
    {
        // Get equivalent CLR type from column type
        var clrType = database.ResolveType(column).GetClrType();
    }
}

Packages

  • CatFactory
  • CatFactory.SqlServer
  • CatFactory.NetCore
  • CatFactory.EntityFrameworkCore
  • CatFactory.AspNetCore
  • CatFactory.Dapper
  • CatFactory.TypeScript

You can check the download statistics for CatFactory packages in NuGet Gallery.

CatFactory

This package provides all definitions for CatFactory engine, this is the core for child packages.

Namespaces:

  • CodeFactory: Contains objects to perform code generation.
  • Diagnostics: Contains objects for diagnostics.
  • Markup: Contains objects for markup languages.
  • ObjectOrientedProgramming: Contains objects to modeling definitions: classes, interfaces and enums.
  • ObjectRelationalMapping: Contains objects for ORM: database, tables, views, scalar functions, table functions and stored procedures.

CatFactory.SqlServer

This packages contains logic to import existing databases from SQL Server instances.

Object Supported
Tables Yes
Views Yes
Scalar Functions Yes
Table Functions Yes
Stored Procedures Yes
Sequences Not yet
Extended Properties Yes
Data types Yes

CatFactory.NetCore

This package contains code builders and definitions for .NET Core (C#).

Object Feature Supported
Interface Inheritance Yes
Interface Events Yes
Interface Properties Yes
Interface Methods Yes
Class Inheritance Yes
Class Events Yes
Class Fields Yes
Class Constructors Yes
Class Properties Yes
Class Methods Yes
Enum Options Yes
Struct All Not yet

CatFactory.EntityFrameworkCore

This package provides scaffolding for Entity Framework Core.

Object Supported
Class for entity Yes
Class for view Yes
Class for table function result Not yet
Class for stored procedure result Not yet
Class for DbContext Yes
Class for entity configuration (table) Yes
Class for entity configuration (view) Yes
Interface for Repository Yes
Class for Repository Yes
Method for scalar function invocation Yes
Method for table function invocation Not yet
Method for stored procedure invocation Not yet
Entity Framework Core 2 Feature Compatibility Chart
Category Feature Supported
Modeling Table splitting Not yet
Modeling Owned types Not yet
Modeling Model-level query filters Not yet
Modeling Database scalar function mapping Not yet
High Performance DbContext pooling Not yet
High Performance Explicitly compiled queries Not yet

Read more here: New features in EF Core 2.0

CatFactory.AspNetCore

This package provides scaffolding for Asp .NET Core.

Feature Supported
Controllers Yes
Requests Yes
Responses Yes
Client Scaffolding Not yet
Help Page for Web API Not yet
Unit Tests Not yet
Not yet

CatFactory.Dapper

This package provides scaffolding for Dapper.

Object Supported
Class for entity Yes
Class for view Yes
Class for table function result Yes
Class for stored procedure result Not yet
Interface for Repository Yes
Class for Repository Yes
Method for scalar function invocation Yes
Method for table function invocation Yes
Method for stored procedure invocation Not yet

CatFactory.TypeScript

This package provides scaffolding for Type Script.

Object Feature Supported
Interface Inheritance Yes
Interface Fields Yes
Interface Properties Yes
Interface Methods Yes
Class Inheritance Yes
Class Fields Yes
Class Constructor Yes
Class Properties Yes
Class Methods Yes
Module All Not yet

Trivia

  • The name for this framework it was F4N1 before than CatFactory
  • Framework's name is related to kitties
  • Import logic uses sp_help stored procedure to retrieve the database object's definition, I learned that in my database course at college
  • Load mapping for entities with MEF, it's inspired in "OdeToCode" (Scott Allen) article for Entity Framework 6.x
  • Expose all settings in one class inside of project's definition is inspired on DevExpress settings for Web controls (Web Forms)
  • This implementation exists thanks to Edson Ferreira and my person

Background

Code generation it's a common technique developers use to reduce time in code writing, I know the most programmers build a code generator in their professional lifes.

EF 6.x had a wizard for code generation, that tool generates DbContext and POCOs but there isn't code for Fluent API, Repositories and other things like those; with .NET Core there is a command line tool for code generation but we have the same scenario, there is generation only for DbContext and Entities; with CatFactory we're looking for a simple way to generate code with enterprise patterns, please don't forget this is an alpha version of CatFactory, don't pretend to have in this date a full version of code generation engine.

Why don't use code CodeDOM? CodeDOM it's a complex code generation engine, I don't saying CodeDOM sucks or something like that, but at this moment we're focus on generate code in the more simple way, maybe in the incoming versions we'll add some integration with CodeDOM.

Prerequisites

Skills

  • OOP
  • AOP
  • ORM
  • C#
  • Design Patterns (Repository and Unit of Work)

Software Prerequisites

  • .NET Core
  • Visual Studio 2017 or VS Code
  • Access to existing SQL Server instance

Using the code

Please follow these steps to scaffold Entity Framework Core with CatFactory:

Step 01 - Create sample database

This is a sample database, Keep in mind you'll change the connection string for your context, also you have to change the name for project and output directory.

Please make sure you have permissions to write in output directory, please make sure about this point that point to avoid common errors.

Sample SQL script:

use master
go

create database Store
go

use Store
go

create schema HumanResources
go

create schema Production
go

create schema Sales
go

create table [dbo].[EventLog]
(
	[EventLogID] uniqueidentifier not null,
	[EventType] int not null,
	[Key] varchar(255) not null,
	[Message] varchar(max) not null,
	[EntryDate] datetime not null
)

create table [dbo].[ChangeLog]
(
	[ChangeLogID] int not null identity(1, 1),
	[ClassName] varchar(255) not null,
	[PropertyName] varchar(255) not null,
	[Key] varchar(255) not null,
	[OriginalValue] varchar(max) null,
	[CurrentValue] varchar(max) null,
	[UserName] varchar(25) not null,
	[ChangeDate] datetime not null
)

create table [dbo].[ChangeLogExclusion]
(
	[ChangeLogExclusionID] int not null identity(1, 1),
	[EntityName] varchar(128) not null,
	[PropertyName] varchar(128) not null
)

create table [dbo].[Country]
(
	[CountryID] int not null,
	[CountryName] varchar(100) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [dbo].[Currency]
(
	[CurrencyID] smallint not null identity(1000, 1000),
	[CurrencyName] varchar(50) not null,
	[CurrencySymbol] varchar(1) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [dbo].[CountryCurrency]
(
	[CountryCurrencyID] int not null identity(1, 1),
	[CountryID] int not null,
	[CurrencyID] smallint not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [HumanResources].[Employee]
(
	[EmployeeID] int not null identity(1, 1),
	[FirstName] varchar(25) not null,
	[MiddleName] varchar(25) null,
	[LastName] varchar(25) not null,
	[BirthDate] datetime not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [HumanResources].[EmployeeAddress]
(
	[EmployeeAddressID] int not null identity(1, 1),
	[EmployeeID] int not null,
	[AddressLine1] varchar(50) not null,
	[AddressLine2] varchar(50) null,
	[City] varchar(25) not null,
	[State] varchar(25) not null,
	[ZipCode] varchar(5) null,
	[CountryID] int not null,
	[PhoneNumber] varchar(25) null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [HumanResources].[EmployeeEmail]
(
	[EmployeeEmailID] int not null identity(1, 1),
	[EmployeeID] int not null,
	[Email] varchar(50) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Production].[ProductCategory]
(
	[ProductCategoryID] int not null identity(1, 1),
	[ProductCategoryName] varchar(100) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Production].[Product]
(
	[ProductID] int not null identity(1, 1),
	[ProductName] varchar(100) not null,
	[ProductCategoryID] int not null,
	[UnitPrice] decimal(8, 4) not null,
	[Description] varchar(255) null,
	[Discontinued] bit not null,
	[Stocks] int not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Production].[Warehouse]
(
	[WarehouseID] varchar(5) not null,
	[WarehouseName] varchar(100) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Production].[ProductInventory]
(
	[ProductInventoryID] int not null identity(1, 1),
	[ProductID] int not null,
	[WarehouseID] varchar(5) not null,
	[Quantity] int not null,
	[Stocks] int not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[OrderStatus]
(
	[OrderStatusID] smallint not null,
	[Description] varchar(100) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[PaymentMethod]
(
	[PaymentMethodID] uniqueidentifier not null,
	[PaymentMethodName] varchar(50) not null,
	[PaymentMethodDescription] varchar(255) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[Customer]
(
	[CustomerID] int not null identity(1, 1),
	[CompanyName] varchar(100) null,
	[ContactName] varchar(100) null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[CustomerAddress]
(
	[CustomerAddressID] int not null identity(1, 1),
	[CustomerID] int not null,
	[AddressLine1] varchar(50) not null,
	[AddressLine2] varchar(50) null,
	[City] varchar(25) not null,
	[State] varchar(25) not null,
	[ZipCode] varchar(5) null,
	[CountryID] int not null,
	[PhoneNumber] varchar(25) null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[CustomerEmail]
(
	[CustomerEmailID] int not null identity(1, 1),
	[CustomerID] int not null,
	[Email] varchar(50) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[Shipper]
(
	[ShipperID] int not null identity(1, 1),
	[CompanyName] varchar(100) null,
	[ContactName] varchar(100) null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[Order]
(
	[OrderID] bigint not null identity(1, 1),
	[OrderStatusID] smallint not null,
	[CustomerID] int not null,
	[EmployeeID] int null,
	[ShipperID] int null,
	[OrderDate] datetime not null,
	[Total] decimal(12, 4) not null,
	[CurrencyID] smallint null,
	[PaymentMethodID] uniqueidentifier null,
	[Comments] varchar(max) null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)

create table [Sales].[OrderDetail]
(
	[OrderDetailID] bigint not null identity(1, 1),
	[OrderID] bigint not null,
	[ProductID] int not null,
	[ProductName] varchar(255) not null,
	[UnitPrice] decimal(8, 4) not null,
	[Quantity] int not null,
	[Total] decimal(8, 4) not null,
	[CreationUser] varchar(25) not null,
	[CreationDateTime] datetime not null,
	[LastUpdateUser] varchar(25) null,
	[LastUpdateDateTime] datetime null,
	[Timestamp] rowversion null
)
go

alter table [dbo].[EventLog]
	add constraint [PK_EventLog] primary key (EventLogID)
go

alter table [dbo].[ChangeLog]
	add constraint [PK_ChangeLog] primary key (ChangeLogID)
go

alter table [dbo].[ChangeLogExclusion]
	add constraint [PK_ChangeLogExclusion] primary key(ChangeLogExclusionID)

alter table [dbo].[Country]
	add constraint [PK_Country] primary key([CountryID])
go

alter table [dbo].[Currency]
	add constraint [PK_Currency] primary key([CurrencyID])
go

alter table [dbo].[CountryCurrency]
	add constraint [PK_CountryCurrency] primary key([CountryCurrencyID])
go

alter table [HumanResources].[Employee]
	add constraint [PK_HumanResources_Employee] primary key (EmployeeID)
go

alter table [HumanResources].[EmployeeAddress]
	add constraint [PK_HumanResources_EmployeeAddress] primary key (EmployeeAddressID)
go

alter table [HumanResources].[EmployeeEmail]
	add constraint [PK_HumanResources_EmployeeEmail] primary key (EmployeeEmailID)
go

alter table [Production].[ProductCategory]
	add constraint [PK_Production_ProductCategory] primary key (ProductCategoryID)
go

alter table [Production].[Product]
	add constraint [PK_Production_Product] primary key (ProductID)
go

alter table [Production].[Product]
	add constraint [U_Production_Product_ProductName] unique (ProductName)
go

alter table [Production].[ProductInventory]
	add constraint [PK_Production_ProductInventory] primary key (ProductInventoryID)
go

alter table [Production].[Warehouse]
	add constraint [PK_Production_Warehouse] primary key (WarehouseID)
go

alter table [Sales].[Customer]
	add constraint [PK_Sales_Customer] primary key (CustomerID)
go

alter table [Sales].[CustomerAddress]
	add constraint [PK_Sales_CustomerAddress] primary key (CustomerAddressID)
go

alter table [Sales].[CustomerEmail]
	add constraint [PK_Sales_CustomerEmail] primary key (CustomerEmailID)
go

alter table [Sales].[Shipper]
	add constraint [PK_Sales_Shipper] primary key (ShipperID)
go

alter table [Sales].[OrderStatus]
	add constraint [PK_Sales_OrderStatus] primary key (OrderStatusID)
go

alter table [Sales].[PaymentMethod]
	add constraint [PK_Sales_PaymentMethod] primary key (PaymentMethodID)
go

alter table [Sales].[Order]
	add constraint [PK_Sales_Order] primary key (OrderID)
go

alter table [Sales].[OrderDetail]
	add constraint [PK_Sales_OrderDetail] primary key (OrderDetailID)
go

alter table [dbo].[CountryCurrency]
	add constraint [U_CountryCurrency] unique (CountryID, CurrencyID)
go

alter table [Sales].[OrderDetail]
	add constraint [U_Sales_OrderDetail] unique (OrderID, ProductID)
go

alter table [ChangeLogExclusion]
	add constraint [U_ChangeLogExclusion] unique(EntityName, PropertyName)
go

alter table [dbo].[CountryCurrency]
	add constraint [FK_CountryCurrency_Country] foreign key (CountryID)
		references [dbo].[Country]
go

alter table [dbo].[CountryCurrency]
	add constraint [FK_CountryCurrency_Currency] foreign key (CurrencyID)
		references [dbo].[Currency]
go

alter table [Production].[Product]
	add constraint [FK_Production_Product_ProductCategory] foreign key (ProductCategoryID)
		references [Production].[ProductCategory]
go

alter table [Production].[ProductInventory]
	add constraint [FK_Production_ProductInventory_Product] foreign key (ProductID)
		references [Production].[Product]
go

alter table [Production].[ProductInventory]
	add constraint [FK_Production_ProductInventory_Warehouse] foreign key (WarehouseID)
		references [Production].[Warehouse]
go

alter table [HumanResources].[EmployeeAddress]
	add constraint [FK_HumanResources_EmployeeAddress] foreign key (EmployeeID)
		references [HumanResources].[Employee]
go

alter table [HumanResources].[EmployeeEmail]
	add constraint [FK_HumanResources_EmployeeEmail] foreign key (EmployeeID)
		references [HumanResources].[Employee]
go

alter table [Sales].[CustomerAddress]
	add constraint [FK_Sales_CustomerAddress] foreign key (CustomerID)
		references [Sales].[Customer]
go

alter table [Sales].[CustomerEmail]
	add constraint [FK_Sales_CustomerEmail] foreign key (CustomerID)
		references [Sales].[Customer]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_OrderStatus] foreign key (OrderStatusID)
		references [Sales].[OrderStatus]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_Customer] foreign key (CustomerID)
		references [Sales].[Customer]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_Employee] foreign key (EmployeeID)
		references [HumanResources].[Employee]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_Shipper] foreign key (ShipperID)
		references [Sales].[Shipper]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_Currency] foreign key (CurrencyID)
		references [dbo].[Currency]
go

alter table [Sales].[Order]
	add constraint [FK_Sales_Order_PaymentMethod] foreign key (PaymentMethodID)
		references [Sales].[PaymentMethod]
go

alter table [Sales].[OrderDetail]
	add constraint [FK_Sales_OrderDetail_Order] foreign key (OrderID)
		references [Sales].[Order]
go

alter table [Sales].[OrderDetail]
	add constraint [FK_Sales_OrderDetail_Product] foreign key (ProductID)
		references [Production].[Product]
go

create function [Production].[ufnGetStock](@productID int)
    returns int
as
    begin
        declare @value int
        select @value = sum([Stocks]) from [Production].[ProductInventory] where [ProductID] = @productID
        if (@value is null)
            set @value = 0
        
        return @value
    end
go

create function [HumanResources].[ufnGetEmployeeFullName](@employeeID int)
    returns varchar(75)
as
    begin
        declare @value varchar(75)

        select
            @value = [FirstName] + isnull(MiddleName + ' ', '') + [LastName]
        from
            [HumanResources].[Employee]
        where
            [employeeID] = @employeeID

        return @value
    end
go

create function [Sales].[ufnGetCustomerContact](@customerID int)
    returns @retCustomerContactInformation table
    (
        [CustomerID] int not null,
        [AddressLine1] varchar(50) not null,
	    [AddressLine2] varchar(50) null,
        [City] varchar(25) not null,
	    [State] varchar(25) not null,
        [ZipCode] varchar(5) null,
	    [PhoneNumber] varchar(25) null
    )
    as
        begin
            insert into
                @retCustomerContactInformation
            select
                [SalesCustomerAddress].[CustomerID],
                [SalesCustomerAddress].[AddressLine1],
                [SalesCustomerAddress].[AddressLine2],
                [SalesCustomerAddress].[City],
                [SalesCustomerAddress].[State],
                [SalesCustomerAddress].[ZipCode],
                [SalesCustomerAddress].[PhoneNumber]
            from
                [Sales].[CustomerAddress] [SalesCustomerAddress]
            where
                [SalesCustomerAddress].[CustomerID] = @customerID
			return
        end
go

create view [HumanResources].[EmployeeInfo]
as
	select
		Employee.FirstName + ' ' + isnull(Employee.MiddleName + ' ', '') + Employee.LastName as EmployeeName,
		(select count(EmployeeID) from HumanResources.EmployeeAddress where EmployeeID = Employee.EmployeeID) as EmployeeAddresses,
		(select count(EmployeeID) from HumanResources.EmployeeEmail where EmployeeID = Employee.EmployeeID) as EmployeeEmails
	from
		HumanResources.Employee Employee
go

create view [Sales].[OrderSummary]
as
	select
	OrderHeader.OrderID,
	Customer.CompanyName as CustomerName,
	Employee.FirstName + ' ' + isnull(Employee.MiddleName + ' ', '') + Employee.LastName as EmployeeName,
	Shipper.CompanyName as ShipperName,
	OrderHeader.OrderDate,
	OrderHeader.Total,
	Currency.CurrencyName,
	PaymentMethod.PaymentMethodName,
	(select count(OrderDetailID) from Sales.OrderDetail where OrderID = OrderHeader.OrderID) as OrderLines
from
	Sales.[Order] OrderHeader
	inner join Sales.Customer Customer
		on OrderHeader.CustomerID = Customer.CustomerID
	inner join HumanResources.Employee Employee
		on OrderHeader.EmployeeID = Employee.EmployeeID
	inner join Sales.Shipper Shipper
		on OrderHeader.ShipperID = Shipper.ShipperID
	inner join dbo.Currency
		on OrderHeader.CurrencyID = Currency.CurrencyID
	inner join Sales.PaymentMethod PaymentMethod
		on OrderHeader.PaymentMethodID = PaymentMethod.PaymentMethodID
go

Step 02 - Create Project

Create a console application for .NET Core, in some cases you can add one project to your existing solution but with some name or sufix that indicates it's a project to scaffold, for example: Store.CatFactory.Dapper.

Add the following packages for your project:

Name Version Description
CatFactory.SqlServer 1.0.0-beta-sun-build21 Provides import feature for SQL Server databases
CatFactory.EntityFrameworkCore 1.0.0-beta-sun-build19 Provides scaffold for Entity Framework Core

Save all changes and build the project.

Step 03 - Add Code to Scaffold

Once we have the packages installed on our project, we can add code in Main method:

// Create database factory
var databaseFactory = new SqlServerDatabaseFactory()
{
    DatabaseImportSettings = new DatabaseImportSettings
    {
        ConnectionString = "server=(local);database=Store;integrated security=yes;",
        ImportScalarFunctions = true,
        Exclusions =
        {
            "dbo.sysdiagrams",
            "dbo.fn_diagramobjects"
        }
    }
};

// Import database
var database = databaseFactory.Import();

// Create instance of Entity Framework Core project
var project = new EntityFrameworkCoreProject
{
    Name = "Store.Core",
    Database = database,
    OutputDirectory = @"C:\Temp\CatFactory.EntityFrameworkCore\Store.Core"
};

// Apply settings for Entity Framework Core project
project.GlobalSelection(settings =>
{
    settings.ForceOverwrite = true;
    settings.AuditEntity = new AuditEntity("CreationUser", "CreationDateTime", "LastUpdateUser", "LastUpdateDateTime");
    settings.ConcurrencyToken = "Timestamp";
});

project.Select("Sales.Order", settings => settings.EntitiesWithDataContracts = true);

// Build features for project, group all entities by schema into a feature
project.BuildFeatures();

// Add event handlers to before and after of scaffold

project.ScaffoldingDefinition += (source, args) =>
{
    // Add code to perform operations with code builder instance before to create code file
};

project.ScaffoldedDefinition += (source, args) =>
{
    // Add code to perform operations after of create code file
};

// Scaffolding =^^=
project
    .ScaffoldEntityLayer()
    .ScaffoldDataLayer();
Extension methods for EntityFrameworkCoreProject instance
Name Description
ScaffoldEntityLayer Scaffold code for entities from tables and views
ScaffoldDataLayer Scaffold code for data access: DbContext, Mapping, Contracts, Data Contracts, Repositories and extensions

Step 04 - Create Console Project

The following code shows how to use the scaffolded code, if We want to use the code with ASP.NET Web API project, We'll need to set up dependency injection and anothers things, please check in related links for more information.

Now We can go to output directory and create a console project for .NET Core, select code snippet according to your case:

Get All:

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new SalesRepository(dbContext))
{
    // Get "in memory" query
    var query = repository.GetOrders();
    
    // Get paging info
    var totalItems = await query.CountAsync();
    var pageSize = 25;
    var pageNumber = 1;
    var pageCount = totalItems / pageSize;
    
    // Retrieve list from database
    var list = await query.Paging(pageSize, pageNumber).ToListAsync();
}

Get by Key:

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new SalesRepository(dbContext))
{
    // Get entity by id
    var entity = await repository.GetOrderAsync(new Order(1));
}

Get by Unique (If table contains an unique constraint):

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new ProductionRepository(dbContext))
{
    // Get entity by name
    var entity = await repository.GetProductByProductNameAsync(new Product { ProductName = "The King of Fighters XIV" });
}

Add:

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new SalesRepository(dbContext))
{
    // Create instance for entity
    var entity = new Order();
    
    // Set values for properties
    // e.g. entity.Total = 29.99m;
    
    // Add entity in database
    await repository.AddOrderAsync(entity);
}

Update:

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new SalesRepository(dbContext))
{
    // Retrieve entity by id instance for entity
    var entity = await repository.GetOrderAsync(new Order(1));
    
    // Set values for properties
    // e.g. entity.Total = 29.99m;
    
    // Add entity in database
    await repository.UpdateOrderAsync(entity);
}

Remove:

// Create options for DbContext instance
var options = new DbContextOptionsBuilder<StoreDbContext>()
    .UseSqlServer("server=(local);database=Store;integrated security=yes;")
    .Options;

// Create DbContext instance
var dbContext = new StoreDbContext(options);

using (var repository = new SalesRepository(dbContext))
{
    // Retrieve entity by id instance for entity
    var entity = await repository.GetOrderAsync(new Order(1));
    
    // Add entity in database
    await repository.RemoveOrderAsync(entity);
}

The previous code samples do not include error handling, these are samples to show the way to work with scaffold code.

How works all code together?

We create a StoreDbContext instance, that instance use the connection string from DbContextOptionsBuilder and inside of OnModelCreating method there is the configuration for all mappings, that's because it's more a stylish way to mapping entities instead of add a lot of lines inside of OnModelCreating

Later, for example we create an instance of SalesRepository passing a valid instance of StoreDbContext and then we can access to repository's operations.

For this architecture implementation we are using the DotNet naming conventions: PascalCase for classes, interfaces and methods; camelCase for parameters.

Namespaces for generated code:

  1. EntityLayer
  2. DataLayer
  3. DataLayer\Contracts
  4. DataLayer\DataContracts
  5. DataLayer\Configurations
  6. DataLayer\Repositories

Inside of EntityLayer we'll place all entities, in this context entity means a class that represents a table or view from database, sometimes entity is named POCO (Plain Old Common language runtime Object) than means a class with only properties not methods nor other things (events).

Inside of DataLayer we'll place DbContext and AppSettings because they're common classes for DataLayer.

Inside of DataLayer\Contracts we'll place all interfaces that represent operations catalog, we're focusing on schemas and we'll create one interface per schema and Store contract for default schema (dbo).

Inside of DataLayer\DataContracts we'll place all object definitions for returned values from Contracts namespace, for now this directory would be empty.

Inside of DataLayer\Configurations we'll place all object definition related to mapping a class for database access.

Inside of DataLayer\Repositories we'll place the implementations for Contracts definitons.

Inside of EntityLayer and DataLayer\Mapping we'll create one directory per schema without include the default schema.

We can review the link about EF Core for enterprise, and we can understand this guide allow to us generate all of that code to reduce time in code writing.

Code Review

We'll review some the output code for one entity to understand the design:

Code for Order class:

using System;
using Store.Core.EntityLayer;
using Store.Core.EntityLayer.Sales;
using Store.Core.EntityLayer.HumanResources;
using System.Collections.ObjectModel;

namespace Store.Core.EntityLayer.Sales
{
 public partial class Order : IAuditEntity
 {
  public Order()
  {
  }

  public Order(long? orderID)
  {
   OrderID = orderID;
  }

  public long? OrderID { get; set; }

  public short? OrderStatusID { get; set; }

  public int? CustomerID { get; set; }

  public int? EmployeeID { get; set; }

  public int? ShipperID { get; set; }

  public DateTime? OrderDate { get; set; }

  public decimal? Total { get; set; }

  public short? CurrencyID { get; set; }

  public Guid? PaymentMethodID { get; set; }

  public string Comments { get; set; }

  public string CreationUser { get; set; }

  public DateTime? CreationDateTime { get; set; }

  public string LastUpdateUser { get; set; }

  public DateTime? LastUpdateDateTime { get; set; }

  public byte[] Timestamp { get; set; }

  public Store.Core.EntityLayer.Currency CurrencyFk { get; set; }

  public Store.Core.EntityLayer.Sales.Customer CustomerFk { get; set; }

  public Store.Core.EntityLayer.HumanResources.Employee EmployeeFk { get; set; }

  public Store.Core.EntityLayer.Sales.OrderStatus OrderStatusFk { get; set; }

  public Store.Core.EntityLayer.Sales.PaymentMethod PaymentMethodFk { get; set; }

  public Store.Core.EntityLayer.Sales.Shipper ShipperFk { get; set; }

  public Collection<OrderDetail> OrderDetailList { get; set; }
 }
}

Code for StoreDbContext class:

using System;
using Microsoft.EntityFrameworkCore;
using Store.Core.EntityLayer;
using Store.Core.DataLayer.Configurations;
using Store.Core.EntityLayer.HumanResources;
using Store.Core.EntityLayer.Production;
using Store.Core.EntityLayer.Sales;
using Store.Core.DataLayer.Configurations.HumanResources;
using Store.Core.DataLayer.Configurations.Production;
using Store.Core.DataLayer.Configurations.Sales;

namespace Store.Core.DataLayer
{
 public class StoreDbContext : DbContext
 {
  public StoreDbContext(DbContextOptions<StoreDbContext> options)
   : base(options)
  {
  }

  public DbSet<ChangeLog> ChangeLogs { get; set; }

  public DbSet<ChangeLogExclusion> ChangeLogExclusions { get; set; }

  public DbSet<Country> Countries { get; set; }

  public DbSet<CountryCurrency> CountryCurrencies { get; set; }

  public DbSet<Currency> Currencies { get; set; }

  public DbSet<EventLog> EventLogs { get; set; }

  public DbSet<Employee> Employees { get; set; }

  public DbSet<EmployeeAddress> EmployeeAddresses { get; set; }

  public DbSet<EmployeeEmail> EmployeeEmails { get; set; }

  public DbSet<Product> Products { get; set; }

  public DbSet<ProductCategory> ProductCategories { get; set; }

  public DbSet<ProductInventory> ProductInventories { get; set; }

  public DbSet<Warehouse> Warehouses { get; set; }

  public DbSet<Customer> Customers { get; set; }

  public DbSet<CustomerAddress> CustomerAddresses { get; set; }

  public DbSet<CustomerEmail> CustomerEmails { get; set; }

  public DbSet<Order> Orders { get; set; }

  public DbSet<OrderDetail> OrderDetails { get; set; }

  public DbSet<OrderStatus> OrderStatuses { get; set; }

  public DbSet<PaymentMethod> PaymentMethods { get; set; }

  public DbSet<Shipper> Shippers { get; set; }

  public DbSet<EmployeeInfo> EmployeeInfos { get; set; }

  public DbSet<OrderSummary> OrderSummaries { get; set; }

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
   // Apply all configurations for tables
   
   modelBuilder
    .ApplyConfiguration(new ChangeLogConfiguration())
    .ApplyConfiguration(new ChangeLogExclusionConfiguration())
    .ApplyConfiguration(new CountryConfiguration())
    .ApplyConfiguration(new CountryCurrencyConfiguration())
    .ApplyConfiguration(new CurrencyConfiguration())
    .ApplyConfiguration(new EventLogConfiguration())
    .ApplyConfiguration(new EmployeeConfiguration())
    .ApplyConfiguration(new EmployeeAddressConfiguration())
    .ApplyConfiguration(new EmployeeEmailConfiguration())
    .ApplyConfiguration(new ProductConfiguration())
    .ApplyConfiguration(new ProductCategoryConfiguration())
    .ApplyConfiguration(new ProductInventoryConfiguration())
    .ApplyConfiguration(new WarehouseConfiguration())
    .ApplyConfiguration(new CustomerConfiguration())
    .ApplyConfiguration(new CustomerAddressConfiguration())
    .ApplyConfiguration(new CustomerEmailConfiguration())
    .ApplyConfiguration(new OrderConfiguration())
    .ApplyConfiguration(new OrderDetailConfiguration())
    .ApplyConfiguration(new OrderStatusConfiguration())
    .ApplyConfiguration(new PaymentMethodConfiguration())
    .ApplyConfiguration(new ShipperConfiguration())
   ;
   
   // Apply all configurations for views
   
   modelBuilder
    .ApplyConfiguration(new EmployeeInfoConfiguration())
    .ApplyConfiguration(new OrderSummaryConfiguration())
   ;
   
   base.OnModelCreating(modelBuilder);
  }

  [DbFunction(FunctionName = "ufnGetEmployeeFullName", Schema = "HumanResources")]
  public static string HumanResourcesUfnGetEmployeeFullName(int? EmployeeID)
  {
   throw new Exception();
  }

  [DbFunction(FunctionName = "ufnGetStock", Schema = "Production")]
  public static int? ProductionUfnGetStock(int? ProductID)
  {
   throw new Exception();
  }
 }
}

Code for OrderConfiguration class:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Store.Core.EntityLayer.Sales;

namespace Store.Core.DataLayer.Configurations.Sales
{
 public class OrderConfiguration : IEntityTypeConfiguration<Store.Core.EntityLayer.Sales.Order>
 {
  public void Configure(EntityTypeBuilder<Store.Core.EntityLayer.Sales.Order> builder)
  {
   // Set configuration for entity
   builder.ToTable("Order", "Sales");
   
   // Set key for entity
   builder.HasKey(p => p.OrderID);
   
   // Set identity for entity (auto increment)
   builder.Property(p => p.OrderID).UseSqlServerIdentityColumn();
   
   // Set configuration for columns
   builder.Property(p => p.OrderID).HasColumnType("bigint").IsRequired();
   builder.Property(p => p.OrderStatusID).HasColumnType("smallint").IsRequired();
   builder.Property(p => p.CustomerID).HasColumnType("int").IsRequired();
   builder.Property(p => p.EmployeeID).HasColumnType("int");
   builder.Property(p => p.ShipperID).HasColumnType("int");
   builder.Property(p => p.OrderDate).HasColumnType("datetime").IsRequired();
   builder.Property(p => p.Total).HasColumnType("decimal(12, 4)").IsRequired();
   builder.Property(p => p.CurrencyID).HasColumnType("smallint");
   builder.Property(p => p.PaymentMethodID).HasColumnType("uniqueidentifier");
   builder.Property(p => p.Comments).HasColumnType("varchar(max)");
   builder.Property(p => p.CreationUser).HasColumnType("varchar(25)").IsRequired();
   builder.Property(p => p.CreationDateTime).HasColumnType("datetime").IsRequired();
   builder.Property(p => p.LastUpdateUser).HasColumnType("varchar(25)");
   builder.Property(p => p.LastUpdateDateTime).HasColumnType("datetime");
   builder.Property(p => p.Timestamp).HasColumnType("timestamp(8)");
   
   // Set concurrency token for entity
   builder
    .Property(p => p.Timestamp)
    .ValueGeneratedOnAddOrUpdate()
    .IsConcurrencyToken();
   
   // Add configuration for foreign keys
   builder
    .HasOne(p => p.CurrencyFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.CurrencyID)
    .HasConstraintName("FK_Sales_Order_Currency");
   
   builder
    .HasOne(p => p.CustomerFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.CustomerID)
    .HasConstraintName("FK_Sales_Order_Customer");
   
   builder
    .HasOne(p => p.EmployeeFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.EmployeeID)
    .HasConstraintName("FK_Sales_Order_Employee");
   
   builder
    .HasOne(p => p.OrderStatusFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.OrderStatusID)
    .HasConstraintName("FK_Sales_Order_OrderStatus");
   
   builder
    .HasOne(p => p.PaymentMethodFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.PaymentMethodID)
    .HasConstraintName("FK_Sales_Order_PaymentMethod");
   
   builder
    .HasOne(p => p.ShipperFk)
    .WithMany(b => b.OrderList)
    .HasForeignKey(p => p.ShipperID)
    .HasConstraintName("FK_Sales_Order_Shipper");
   
  }
 }
}

Code for ISalesRepository interface:

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Store.Core.EntityLayer;
using Store.Core.DataLayer.Contracts;
using Store.Core.EntityLayer.HumanResources;
using Store.Core.EntityLayer.Production;
using Store.Core.EntityLayer.Sales;
using Store.Core.DataLayer.DataContracts;

namespace Store.Core.DataLayer.Contracts
{
 public interface ISalesRepository : IRepository
 {
  IQueryable<Customer> GetCustomers();

  Task<Customer> GetCustomerAsync(Customer entity);

  IQueryable<CustomerAddress> GetCustomerAddresses(int? customerID = null);

  Task<CustomerAddress> GetCustomerAddressAsync(CustomerAddress entity);

  IQueryable<CustomerEmail> GetCustomerEmails(int? customerID = null);

  Task<CustomerEmail> GetCustomerEmailAsync(CustomerEmail entity);

  IQueryable<OrderDto> GetOrders(short? currencyID = null, int? customerID = null, int? employeeID = null, short? orderStatusID = null, Guid? paymentMethodID = null, int? shipperID = null);

  Task<Order> GetOrderAsync(Order entity);

  IQueryable<OrderDetail> GetOrderDetails(long? orderID = null, int? productID = null);

  Task<OrderDetail> GetOrderDetailAsync(OrderDetail entity);

  Task<OrderDetail> GetOrderDetailByOrderIDAndProductIDAsync(OrderDetail entity);

  IQueryable<OrderStatus> GetOrderStatuses();

  Task<OrderStatus> GetOrderStatusAsync(OrderStatus entity);

  IQueryable<PaymentMethod> GetPaymentMethods();

  Task<PaymentMethod> GetPaymentMethodAsync(PaymentMethod entity);

  IQueryable<Shipper> GetShippers();

  Task<Shipper> GetShipperAsync(Shipper entity);

  IQueryable<OrderSummary> GetOrderSummaries();
 }
}

Code for OrderDataContract class:

using System;

namespace Store.Core.DataLayer.DataContracts
{
 public class OrderDto
 {
  public Int64? OrderID { get; set; }

  public Int16? OrderStatusID { get; set; }

  public Int32? CustomerID { get; set; }

  public Int32? EmployeeID { get; set; }

  public Int32? ShipperID { get; set; }

  public DateTime? OrderDate { get; set; }

  public Decimal? Total { get; set; }

  public Int16? CurrencyID { get; set; }

  public Guid? PaymentMethodID { get; set; }

  public String Comments { get; set; }

  public String CreationUser { get; set; }

  public DateTime? CreationDateTime { get; set; }

  public String LastUpdateUser { get; set; }

  public DateTime? LastUpdateDateTime { get; set; }

  public Byte[] Timestamp { get; set; }

  public String CurrencyCurrencyName { get; set; }

  public String CurrencyCurrencySymbol { get; set; }

  public String CustomerCompanyName { get; set; }

  public String CustomerContactName { get; set; }

  public String EmployeeFirstName { get; set; }

  public String EmployeeMiddleName { get; set; }

  public String EmployeeLastName { get; set; }

  public DateTime? EmployeeBirthDate { get; set; }

  public String OrderStatusDescription { get; set; }

  public String PaymentMethodPaymentMethodName { get; set; }

  public String PaymentMethodPaymentMethodDescription { get; set; }

  public String ShipperCompanyName { get; set; }

  public String ShipperContactName { get; set; }
 }
}

Code for Repository class:

using System;
using System.Threading.Tasks;
using Store.Core.EntityLayer;

namespace Store.Core.DataLayer.Contracts
{
 public class Repository
 {
  protected bool Disposed;
  protected StoreDbContext DbContext;

  public Repository(StoreDbContext dbContext)
  {
   DbContext = dbContext;
  }

  public void Dispose()
  {
   if (!Disposed)
   {
    DbContext?.Dispose();
   
    Disposed = true;
   }
  }

  public virtual void Add<TEntity>(TEntity entity) where TEntity : class
  {
   // Cast entity to IAuditEntity
   if(entity is IAuditEntity cast)
   {
    // Set creation datetime
    cast.CreationDateTime = DateTime.Now;
   }
   
   DbContext.Add(entity);
  }

  public virtual void Update<TEntity>(TEntity entity) where TEntity : class
  {
   // Cast entity to IAuditEntity
   if (entity is IAuditEntity cast)
   {
    // Set update datetime
    cast.LastUpdateDateTime = DateTime.Now;
   }
   
   DbContext.Update(entity);
  }

  public virtual void Remove<TEntity>(TEntity entity) where TEntity : class
  {
   DbContext.Remove(entity);
  }

  public int CommitChanges()
   => DbContext.SaveChanges();

  public Task<int> CommitChangesAsync()
   => DbContext.SaveChangesAsync();
 }
}

Code for SalesRepository class:

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Store.Core.EntityLayer;
using Store.Core.DataLayer.Contracts;
using Store.Core.EntityLayer.HumanResources;
using Store.Core.EntityLayer.Production;
using Store.Core.EntityLayer.Sales;
using Store.Core.DataLayer.DataContracts;

namespace Store.Core.DataLayer.Repositories
{
 public class SalesRepository : Repository, ISalesRepository
 {
  public SalesRepository(StoreDbContext dbContext)
   : base(dbContext)
  {
  }

  public IQueryable<Customer> GetCustomers()
   => DbContext.Customers;

  public async Task<Customer> GetCustomerAsync(Customer entity)
   => await DbContext.Customers.FirstOrDefaultAsync(item => item.CustomerID == entity.CustomerID);

  public IQueryable<CustomerAddress> GetCustomerAddresses(int? customerID = null)
  {
   // Get query from DbSet
   var query = DbContext.CustomerAddresses.AsQueryable();
   
   // Filter by: 'CustomerID'
   if (customerID.HasValue)
    query = query.Where(item => item.CustomerID == customerID);
   
   return query;
  }

  public async Task<CustomerAddress> GetCustomerAddressAsync(CustomerAddress entity)
   => await DbContext.CustomerAddresses.FirstOrDefaultAsync(item => item.CustomerAddressID == entity.CustomerAddressID);

  public IQueryable<CustomerEmail> GetCustomerEmails(int? customerID = null)
  {
   // Get query from DbSet
   var query = DbContext.CustomerEmails.AsQueryable();
   
   // Filter by: 'CustomerID'
   if (customerID.HasValue)
    query = query.Where(item => item.CustomerID == customerID);
   
   return query;
  }

  public async Task<CustomerEmail> GetCustomerEmailAsync(CustomerEmail entity)
   => await DbContext.CustomerEmails.FirstOrDefaultAsync(item => item.CustomerEmailID == entity.CustomerEmailID);

  public IQueryable<OrderDto> GetOrders(short? currencyID = null, int? customerID = null, int? employeeID = null, short? orderStatusID = null, Guid? paymentMethodID = null, int? shipperID = null)
  {
   // Get query from DbSet
   var query = from order in DbContext.Set<Order>()
    join currencyJoin in DbContext.Currencies on order.CurrencyID equals currencyJoin.CurrencyID into currencyTemp
     from currency in currencyTemp.DefaultIfEmpty()
    join customer in DbContext.Customers on order.CustomerID equals customer.CustomerID
    join employeeJoin in DbContext.Employees on order.EmployeeID equals employeeJoin.EmployeeID into employeeTemp
     from employee in employeeTemp.DefaultIfEmpty()
    join orderStatus in DbContext.OrderStatuses on order.OrderStatusID equals orderStatus.OrderStatusID
    join paymentMethodJoin in DbContext.PaymentMethods on order.PaymentMethodID equals paymentMethodJoin.PaymentMethodID into paymentMethodTemp
     from paymentMethod in paymentMethodTemp.DefaultIfEmpty()
    join shipperJoin in DbContext.Shippers on order.ShipperID equals shipperJoin.ShipperID into shipperTemp
     from shipper in shipperTemp.DefaultIfEmpty()
    select new OrderDto
    {
     OrderID = order.OrderID,
     OrderStatusID = order.OrderStatusID,
     CustomerID = order.CustomerID,
     EmployeeID = order.EmployeeID,
     ShipperID = order.ShipperID,
     OrderDate = order.OrderDate,
     Total = order.Total,
     CurrencyID = order.CurrencyID,
     PaymentMethodID = order.PaymentMethodID,
     Comments = order.Comments,
     CreationUser = order.CreationUser,
     CreationDateTime = order.CreationDateTime,
     LastUpdateUser = order.LastUpdateUser,
     LastUpdateDateTime = order.LastUpdateDateTime,
     Timestamp = order.Timestamp,
     CurrencyCurrencyName = currency == null ? string.Empty : currency.CurrencyName,
     CurrencyCurrencySymbol = currency == null ? string.Empty : currency.CurrencySymbol,
     CustomerCompanyName = customer == null ? string.Empty : customer.CompanyName,
     CustomerContactName = customer == null ? string.Empty : customer.ContactName,
     EmployeeFirstName = employee == null ? string.Empty : employee.FirstName,
     EmployeeMiddleName = employee == null ? string.Empty : employee.MiddleName,
     EmployeeLastName = employee == null ? string.Empty : employee.LastName,
     EmployeeBirthDate = employee == null ? default(DateTime?) : employee.BirthDate,
     OrderStatusDescription = orderStatus == null ? string.Empty : orderStatus.Description,
     PaymentMethodPaymentMethodName = paymentMethod == null ? string.Empty : paymentMethod.PaymentMethodName,
     PaymentMethodPaymentMethodDescription = paymentMethod == null ? string.Empty : paymentMethod.PaymentMethodDescription,
     ShipperCompanyName = shipper == null ? string.Empty : shipper.CompanyName,
     ShipperContactName = shipper == null ? string.Empty : shipper.ContactName,
    };
   
   // Filter by: 'CurrencyID'
   if (currencyID.HasValue)
    query = query.Where(item => item.CurrencyID == currencyID);
   
   // Filter by: 'CustomerID'
   if (customerID.HasValue)
    query = query.Where(item => item.CustomerID == customerID);
   
   // Filter by: 'EmployeeID'
   if (employeeID.HasValue)
    query = query.Where(item => item.EmployeeID == employeeID);
   
   // Filter by: 'OrderStatusID'
   if (orderStatusID.HasValue)
    query = query.Where(item => item.OrderStatusID == orderStatusID);
   
   // Filter by: 'PaymentMethodID'
   if (paymentMethodID != null)
    query = query.Where(item => item.PaymentMethodID == paymentMethodID);
   
   // Filter by: 'ShipperID'
   if (shipperID.HasValue)
    query = query.Where(item => item.ShipperID == shipperID);
   
   return query;
  }

  public async Task<Order> GetOrderAsync(Order entity)
  {
   return await DbContext.Orders
    .Include(p => p.CurrencyFk)
    .Include(p => p.CustomerFk)
    .Include(p => p.EmployeeFk)
    .Include(p => p.OrderStatusFk)
    .Include(p => p.PaymentMethodFk)
    .Include(p => p.ShipperFk)
    .FirstOrDefaultAsync(item => item.OrderID == entity.OrderID);
  }

  public IQueryable<OrderDetail> GetOrderDetails(long? orderID = null, int? productID = null)
  {
   // Get query from DbSet
   var query = DbContext.OrderDetails.AsQueryable();
   
   // Filter by: 'OrderID'
   if (orderID.HasValue)
    query = query.Where(item => item.OrderID == orderID);
   
   // Filter by: 'ProductID'
   if (productID.HasValue)
    query = query.Where(item => item.ProductID == productID);
   
   return query;
  }

  public async Task<OrderDetail> GetOrderDetailAsync(OrderDetail entity)
   => await DbContext.OrderDetails.FirstOrDefaultAsync(item => item.OrderDetailID == entity.OrderDetailID);

  public async Task<OrderDetail> GetOrderDetailByOrderIDAndProductIDAsync(OrderDetail entity)
   => await DbContext.OrderDetails.FirstOrDefaultAsync(item => item.OrderID == entity.OrderID && item.ProductID == entity.ProductID);

  public IQueryable<OrderStatus> GetOrderStatuses()
   => DbContext.OrderStatuses;

  public async Task<OrderStatus> GetOrderStatusAsync(OrderStatus entity)
   => await DbContext.OrderStatuses.FirstOrDefaultAsync(item => item.OrderStatusID == entity.OrderStatusID);

  public IQueryable<PaymentMethod> GetPaymentMethods()
   => DbContext.PaymentMethods;

  public async Task<PaymentMethod> GetPaymentMethodAsync(PaymentMethod entity)
   => await DbContext.PaymentMethods.FirstOrDefaultAsync(item => item.PaymentMethodID == entity.PaymentMethodID);

  public IQueryable<Shipper> GetShippers()
   => DbContext.Shippers;

  public async Task<Shipper> GetShipperAsync(Shipper entity)
   => await DbContext.Shippers.FirstOrDefaultAsync(item => item.ShipperID == entity.ShipperID);

  public IQueryable<OrderSummary> GetOrderSummaries()
   => DbContext.OrderSummaries;
 }
}

AuditEntity in settings sets the columns for audit, this version of CatFactory supports creation and last update for audit.

ConcurrencyToken sets the column for concurrency, this value will be use in entity's mapping.

Don't forget, the previous settings are about columns, we need to use the name of columns not the properties.

EntitiesWithDataContracts indicates that entities will scaffold with joins in linq, this means CatFactory engine reads all foreign keys and create a data contract to retrieve information, if we take a look on GetOrders method on SalesRepository, we can see a linq query with data contract and not a lambda expression as GetShippers method.

Please take a look on all operations in repositories, they are async operations.

Setting Up CatFactory for Entity Framework Core

Additionally, there are more settings for Entity Framework Core project instance, we'll take a look on those settings:

Name Default Value Description
ForceOverwrite false Indicates if code builder must overwrite files if they already exist
SimplifyDataTypes true Indicates if code builder must change from CLR types to native types (e.g. Int32 => int)
UseAutomaticPropertiesForEntities true Indicates if entities classes will use automatic properties or not, if value is false, the entity will contains private fields
EnableDataBindings false Implements INotifyPropertyChanged property and add properties with fields for class definition
UseDataAnnotations false Indicates if mapping in EF Core it will be with data annotations
DeclareDbSetPropertiesInDbContext false Indicates if DbContext class definition must to contains declaration of DbSet
DeclareNavigationPropertiesAsVirtual false Indicates if navigation propeties must to declare as virtual
NavigationPropertyEnumerableNamespace System.Collections.ObjectModel Sets the namespace for navigation properties types
NavigationPropertyEnumerableType Collection Sets the type for collection navigation properties
ConcurrencyToken   Sets the column name that respresents the concurrency token
EntityInterfaceName IEntity Sets the name for entity interface
AuditEntity   Sets the names for audit column: creation and last update (user name and date)
EntitiesWithDataContracts false Indicates if entities must to scaffold as data transfer objects in repositories
BackingFields Sets the name of columns will use fields instead of properties
InsertExclusions Sets the name of columns will ignore for insert action
UpdateExclusions Sets the name of columns will ignore for update action

Also we can change the namespaces, set the values for output namespaces inside of project instance:

Namespace Default Value Description
Entity Layer EntityLayer Gets or sets the namespace for entity layer
Data Layer DataLayer Gets or sets the namespace for data layer
Configurations Configurations Gets or sets the namespace for configurations in data layer
Contracts Contracts Gets or sets the namespace for contracts in data layer
Data Contracts DataContracts Gets or sets the namespace for data contracts in data layer
Repositories Repositories Gets or sets the namespace for repositories in data layer

Points of Interest

  • CatFactory doesn't have command line for nuget because from my point of view it will be a big trouble to allow set values for all settings because we have a lot of settings for EntityFrameworkCoreProjectSettings, I think at this moment is more simple to create a console project to generate the code and then developer move generated files for existing project and make a code refactor if applies
  • CatFactory doesn't have UI now because at the beginning of this project .NET Core had no an standard UI, but we're working on UI for CatFactory maybe we'll choose Angular =^^=
  • Now we are focused on Entity Framework Core and Dapper but in future there will be Web API, Unit Tests and other things :)
  • CatFactory has a package for Dapper, at this moment there isn't article for that but the way to use is similar for Entity Framework Core; you can install CatFactory.Dapper package from nuget
  • We're working in continuous updates to provide better help for user

Related Links

Code Improvements

  • Add support for table functions
  • Add support for stored procedures
  • Allow to overwrite naming convention for project
  • Add author's information for output files

Bugs?

If you get any exception with CatFactory packages, please use these links:

History

  • 12th December, 2016: Initial version
  • 16th December, 2016: Addition of script for database
  • 30th January, 2017: Addition of async operations
  • 12th March, 2017: Addition of audit entity, concurrency token and entities with data contracts
  • 4th June, 2017: Addition of backing fields, data bindings and using of MEF for loading entities mapping
  • 19th September, 2017: Change on article's sections
  • 31th October, 2017: Update to alpha 3 version
  • 21th November, 2017: Addition of Trivia
  • 18th January, 2018: Addition of Workshop
  • 30th Abril, 2018: Update package to beta version
  • 16th May, 2018: Add support for EF Core 2
  • 30th October, 2018: Refactor for article's sections

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

HHerzl
Software Developer
United States United States
Full Stack Developer with Experience in ASP.NET Core and Angular

You may also be interested in...

Comments and Discussions

 
QuestionLooks great Pin
David Schiffer31-Oct-18 9:46
professionalDavid Schiffer31-Oct-18 9:46 
AnswerRe: Looks great Pin
HHerzl7-Nov-18 22:08
memberHHerzl7-Nov-18 22:08 
SuggestionImprovement and suggestions Pin
Member 965484630-Nov-17 1:23
memberMember 965484630-Nov-17 1:23 
GeneralRe: Improvement and suggestions Pin
HHerzl18-Dec-17 18:03
memberHHerzl18-Dec-17 18:03 
PraiseRe: Improvement and suggestions Pin
Member 965484620-Dec-17 8:08
memberMember 965484620-Dec-17 8:08 
BugPrimary keys Pin
Member 965484629-Nov-17 1:25
memberMember 965484629-Nov-17 1:25 
GeneralRe: Primary keys Pin
HHerzl3-Dec-17 18:39
memberHHerzl3-Dec-17 18:39 
GeneralRe: Primary keys Pin
Member 96548464-Dec-17 8:11
memberMember 96548464-Dec-17 8:11 
GeneralRe: Primary keys Pin
HHerzl5-Dec-17 3:19
memberHHerzl5-Dec-17 3:19 
GeneralRe: Primary keys Pin
HHerzl18-Dec-17 14:26
memberHHerzl18-Dec-17 14:26 
Bugviews Pin
Member 965484629-Nov-17 0:50
memberMember 965484629-Nov-17 0:50 
GeneralRe: views Pin
HHerzl3-Dec-17 18:33
memberHHerzl3-Dec-17 18:33 
GeneralRe: views Pin
Member 96548464-Dec-17 8:13
memberMember 96548464-Dec-17 8:13 
BugAn other issue Pin
Member 965484628-Nov-17 22:45
memberMember 965484628-Nov-17 22:45 
GeneralRe: An other issue Pin
HHerzl5-Dec-17 3:14
memberHHerzl5-Dec-17 3:14 
GeneralRe: An other issue Pin
HHerzl18-Dec-17 14:25
memberHHerzl18-Dec-17 14:25 
QuestionGreat Job :-) Pin
Member 96548466-Nov-17 0:27
memberMember 96548466-Nov-17 0:27 
AnswerRe: Great Job :-) Pin
HHerzl7-Nov-17 17:57
memberHHerzl7-Nov-17 17:57 
AnswerRe: Great Job :-) Pin
HHerzl8-Nov-17 22:47
memberHHerzl8-Nov-17 22:47 
PraiseRe: Great Job :-) Pin
Member 96548469-Nov-17 0:38
memberMember 96548469-Nov-17 0:38 
GeneralRe: Great Job :-) Pin
HHerzl20-Nov-17 15:11
memberHHerzl20-Nov-17 15:11 
PraiseRe: Great Job :-) Pin
Member 965484624-Nov-17 0:54
memberMember 965484624-Nov-17 0:54 
GeneralRe: Great Job :-) Pin
Member 965484628-Nov-17 22:48
memberMember 965484628-Nov-17 22:48 
QuestionGreat article & concept, but a little problem Pin
dkurok5-Nov-17 2:58
memberdkurok5-Nov-17 2:58 
AnswerSolved: Great article & concept, but a little problem Pin
dkurok5-Nov-17 3:50
memberdkurok5-Nov-17 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.181117.1 | Last Updated 30 Oct 2018
Article Copyright 2016 by HHerzl
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid