DbContextCRUDExtensions 1.0.9

dotnet add package DbContextCRUDExtensions --version 1.0.9
NuGet\Install-Package DbContextCRUDExtensions -Version 1.0.9
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DbContextCRUDExtensions" Version="1.0.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DbContextCRUDExtensions --version 1.0.9
#r "nuget: DbContextCRUDExtensions, 1.0.9"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DbContextCRUDExtensions as a Cake Addin
#addin nuget:?package=DbContextCRUDExtensions&version=1.0.9

// Install DbContextCRUDExtensions as a Cake Tool
#tool nuget:?package=DbContextCRUDExtensions&version=1.0.9

DbContext Create and Search Extensions

This is a .Net Core package for dealing with creating and searching entities with EFCore Version 7.0.13.

Example Project

You can view examples of each of these methods and different ways to use them in this repository.

Installation

CLI dotnet add package DbContextCRUDExtensions --version <version_number>

Nuget Package Manager

  • Search DBContextCRUDExtensions
  • Select projects to install to and version number to install
  • Click Install

DBContext Extension Methods

Method Name Description
CreateEntityAsync Creates an entity asynchronously
CreateEntity Creates an entity synchronously
Search Searches the database synchrnously
SearchAsync Searches the database asynchrnously

CreateEntityAsync

This method creates an entity and automatically adds it to the DBContext change tracker and returns the entity created.

CreateEntityAsync has two overloads.

Both overloads take in 2 type parameters

  1. The Entity you want to create
  2. The Data Transfer Object you wish to convert from

The first overload takes in 3 parameters

  1. The Data Transfer Object you wish to convert to an entity
  2. A Method that takes in an instance of your dto, and returns Task<Entity>. This version is awaitable in case you need to perform an asynchronous action in the conversion method.
  3. A boolean to indicate if you would like to save the change to the database. This defaults to false. SaveChanges should only be called once during a request. It is recommended to make all of the changes to your entities, and then save

The Second overload also takes in 3 parameters

  1. The Data Transfer Object you wish to convert to an entity
  2. A Method that takes in an instance of your dto, and returns the entity object
  3. A boolean to indicate if you would like to save the change to the database. This defaults to false. SaveChanges should only be called once during a request. It is recommended to make all of the changes to your entities, and then save

This method returns the entity that was created and added to the changetracker.

CreateEntity

This method creates an entity and automatically adds it to the DBContext change tracker and returns the entity created.

CreateEntity is a synchrnous method that takes in 2 type parameters

  1. The Entity you want to create
  2. The Data Transfer Object you wish to convert from

It also takes in 3 parameters

  1. The Data Transfer Object you wish to convert to an entity
  2. A Method that takes in an instance of your dto, and returns the entity object
  3. A boolean to indicate if you would like to save the change to the database. This defaults to false. SaveChanges should only be called once during a request. It is recommended to make all of the changes to your entities, and then save

This is just a synchrnous version of CreateEntityAsync.

SearchAsync

This method queries a database using given pagination and filter data. and returns a PageReturn object.

It takes in 3 type parameters

  1. Entity you wish to search through.
  2. The Data Transfer Object you want to convert the results to.
  3. The filter object you want to filter by.

It also takes in 4 parameters

  1. A PageAndFilter Object
  2. Method that takes in an object of you entity, and returns the dto you want to convert to.
  3. Method that takes in your filter object and an IQueryable of your entity type and returns an Iqueryable of your entity type. This can be null if you don't want to provide an implementation for this method. If null, no filters will be applied.
  4. A nullable string array to add your includes on your query for your navigation objects.

This method is exactly the same as SearchAsync, but is synchronous. All type parameters and parameters are the same.

Enums

SortDirection

The only Enum in this package is the SortDirection Enum. This enum has 2 values

  • ASC (for Ascending)
  • DSC (for Descending)

This can be used however you wish for sorting, but is implemented in this package in the search methods to determine what sort order to use on a given property.

Classes

Page

Page has 4 properties.

Type Name Description
int PageNumber Indicates which page number you want
int RecordsPerPage Indicates how many records you want to return
SortDirection SortDirection Is used to determine if the property is sorted in ascending or descending order
string SortField Determines which field the resulting data will be sorted by

This class has one constructor that sets PageNumber to 0, RecordsPerPage to 25, SortDirection to Ascending, and SortField to "Id"

PageAndFilter

PageAndFitler has 1 type parameter T.

  1. The class you want to use for your filter object.

PageAndFilter has 2 properties

Type Name Description
Page Page The Page object you want to use for paginating search results
T Filter The Filter object you want to use for filtering search results

PageAndFilter also has 4 shortcuts for properties on the Page object

Name Return
PageNumber Page.PageNumber
RecordsPerPage Page.RecordsPerPage
SortDirection Page.SortDirection
SortField Page.SortDirection

PageReturn

This is the return object for both Search and SearchAsync methods.

PageReturn has 2 type parameters

  1. The Data Transfer Object
  2. The Filter object that you want to use.

PageReturn has 3 properties

Type Name Description
List Data The resulting data from a search
int TotalRecordCount Total number of records that match filter, before pagination
PageAndFilter PageAndFilter PageAndFilter object that was used for Search requests
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.9 217 11/7/2023
1.0.8 123 7/26/2023
1.0.7 145 6/28/2023