Skip to content

EF Core database

Jon P Smith edited this page Apr 10, 2018 · 1 revision

The GitHub repo's example ASP.NET Core Razor Pages application uses a EF Core database structure defined in the project called DataLayer.

This page describes the types of entity classes (entity classes is the name given to classes that are mapped by EF Core to a database). GenericServices classified entity types into two main styles:

  1. Standard styled: These are entity classes that can be created/updated by setting their properties. Useful for simple entity classes where there is no business logic or special relationships.
  2. DDD-styled: These are entity classes where you create/update by public methods or constructors. Useful for entity classes that have business rules on how to create/update, and may be the root of a set of aggregate entity types (read this part of the article for more on these DDD terms).

If you are not familiar with Domain-Driven Design (DDD) entity classes I recommend you read the article Creating Domain-Driven Design entity classes with Entity Framework Core first.

Note: there are other styles, like ReadOnly (obvious) and Hybrid (both standard and DDD elements in an entity).

GenericServices is designed to work with both types, and the example database contains both of the main types to show how they are used.

Standard type entity classes

I have styled the Author as a standard entity class. The Author class is simple, with just a Author Name and Email address.

You can use this style of entity class directly in GenericServices, e.g.

var author = new Author{ Name = "Pete", Email = "Pete@nospam.com"}
_service.CreateAndSave(author );

If you use a DTO then GenericServices will use AutoMapper to update the properties.

DDD-styled entity classes

I have styled the Book as a DDD-styled entity class, with the Review and BookAuthor as the Book's aggregates entity classes (The Review and BookAuthor classes are ReadOnly-styled so that only the Book entity can create/update them).

The only way you can create/update a DDD-styled entity classes is via DTOs. GenericServices will match the properties in the DTO to the parameters in the methods/constructors and then build LINQ commands to call the method (which is very fast).