RepoDb 1.0.5

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package RepoDb --version 1.0.5
NuGet\Install-Package RepoDb -Version 1.0.5
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="RepoDb" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RepoDb --version 1.0.5
#r "nuget: RepoDb, 1.0.5"
#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 RepoDb as a Cake Addin
#addin nuget:?package=RepoDb&version=1.0.5

// Install RepoDb as a Cake Tool
#tool nuget:?package=RepoDb&version=1.0.5

How to use RepoDb?

Creating an Entity

  • Map attribute defines which object to use in the database. The value of this attribute could be the actual name of the table or a stored procedure
  • Map attribute second parameter is a CommandType. It tells whether the object is for TableDirect, StoredProcedure or Text base execution.
  • Ignore attribute is a property level attribute used to signify whether the property will be ignore on a certain commands (SELECT, DELETE, UPDATE, INSERT, MERGE)
  • Primary attribute is used to flag a property as the primary field of the table (or a result set).
public interface IStock : IDataEntity

{
	int Id { get; set; }
	string ProductId { get; set; }
	...
	DateTime CreatedDate { get; set; }
	DateTime ModifiedDate { get; set; }
}

[Map("[dbo].[Stock]")]
public class Stock : DataEntity
{
	[Ignore(Command.Insert | Command.Update)]
	[Primary]
	public int Id { get; set; }
	
	...
	
	[Ignore(Command.Insert | Command.Update)]
	public DateTime CreatedDate { get; set; }

	[Ignore(Command.Insert)]
	public DateTime ModifiedDate { get; set; }
}

Creating a repository

  • The class must inherit the BaseRepository<TEntity, TDbConnection> or DbRepository<TDbConnection>.
  • The TEntity dynamic parameter type is used to define which entity objects to used on this repository.
  • The TDbConnection dynamic parameter type is used to define the type of Connection object to be used when connecting to the database (SqlServer, Oracle, SqlLite, etc etc).
  • The constructor of the BaseRepository accepts two parameters, the ConnectionString and the CommandTimeout.
public class StockRepository : BaseRepository<Stock, SqlConnection>
{
	public StockRepository(ISettings settings)
		: base(settings.ConnectionString)
	{
	}
}

Querying a data

  • The first query will return the Stock data from the database where the Id is equals to 256;
  • The second query will return the Stock data from the database where the Id is greater than or equals to 50;
var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);

Dynamic approach

var stock = stockRepository.Query({ Id = 256 });

Explicit approach

var stock = stockRepository.Query(new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 50) });

Updating a data

var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });
stock.Name = $"{stock.Name} - some updates";

Default approach, uses the PrimaryKey

var affectedRows = stockRepository.Update(stock);

Dynamic approach

var affectedRows = stockRepository.Update(stock, new { Id = 256 });

Explicit approach

var affectedRows = stockRepository.Update(stock, new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 50) });

Deleting a data

var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });

Dynamic approach

var affectedRows = stockRepository.Delete({ Id = 256 });

Explicit approach

var affectedRows = stockRepository.Delete(stock, new List<IQueryField>() { new QueryField("Id", Operation.GreaterThanOrEqual, 256) });

Merging a data

var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stock = stockRepository.Query({ Id = 256 });
stock.Name = $"{stock.Name} - some merge updates";

Default approach, uses the PrimaryKey as the qualifiers

var affectedRows = stockRepository.Merge(stock);

Explicit approach, uses the ProductId and Code fields as the qualifiers

var affectedRows = stockRepository.Merge(stock, Field.From("ProductId", "Code"));

Bulk inserting a data

var settings = new Settings();
var stockRepository = new StockRepository(settings.ConnectionString);
var stocks = new List<Stock>();

stocks.Add(new Stock() { ... });
...
stocks.Add(new Stock() { ... });
var affectedRows = stockRepository.BulkInsert(stocks);

You can as well use the DbRepository<TDbConnection> object to avoid the binding on the Entity level repository. With this object, you can dynamically manipulate (CRUD) all the objects or entities that you wish to manipulate from the database.

Explicit Executions

var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);

Explicit ExecuteNonQuery Below is the way on how to delete a Stock with Id equals to 1

var stocks = repository.CreateConnection().ExecuteNonQuery("DELETE FROM [dbo].[Stock] WHERE (Id = @Id);", new { Id = 1 });

Explicit ExecuteReader

var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);

Suppose you have a Product table, it will return a Product with Name equals "Dairy Milk"

var product = repository.Query<Product>({ Name = "Dairy Milk" });

Get the stock of the Product milk in dynamic approach

var stock = repository.Query<Stock>({ ProductId = product.Id });

Use a SQL Statement directly to return the result at the desired object

var productsByDate = repository.CreateConnection().ExecuteReader<Product>("SELECT * FROM [dbo].[Product] WHERE (TransactionDate = @TransactionDate);", new { TransactionDate = DateTime.Parse("2018-01-01 00:00:00.000") });

Use the extended version of the ExecuteReader named ExecuteReaderEx to return the dynamic objects

var stocks = repository.CreateConnection().ExecuteReader("SELECT * FROM [dbo].[Stock];");
stocks.ForEach(stock => {
	var current = (dynamic)product;
	var name = current.Name;
});

Transactions with RepoDb

var settings = new Settings();
var repository = new DbRepository<SqlConnection>(settings.ConnectionString, settings.CommandTimeout);
using (var connection = repository.CreateConnection())
{
	using (var transaction = connection.BeginTransaction())
	{
		repository.ExecuteNonQuery("DELETE FROM [dbo].[Stock] WHERE CreatedDate > @CreatedDate);", new { CreatedDate = DateTime.Parse("2017-01-01") });
		var stocks = repository.Query<Stock>({ ProductId = 12 });
		stocks.ForEach(stock => {
			stock.Quantity = 100;
		});
		repository.Merge<Stock>(stocks);
		transaction.RollBack(); // RollBack Everything
	}
}

Threading with RepoDb

Lastly, the RepoDb also supports the Threading by calling each "Async" method on the repository and on the DbConnection extensions. Below are the following methods that are Multi-Threaded.

  • QueryAsync
  • DeleteAsync
  • InsertAsync
  • UpdateAsync
  • MergeAsync
  • BulkInsertAsync
  • ExecuteNonQueryAsync
  • ExecuteScalarAsync
  • ExecuteReaderAsync
  • ExecuteReaderExAsync
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (17)

Showing the top 5 NuGet packages that depend on RepoDb:

Package Downloads
RepoDb.SqlServer The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A hybrid .NET ORM library for SQL Server.

RepoDb.SqlServer.BulkOperations The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library that contains the official Bulk Operations of RepoDb for SQL Server.

RepoDb.PostgreSql The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A hybrid .NET ORM library for PostgreSQL.

RepoDb.MySql The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A hybrid .NET ORM library for MySQL (using MySql.Data).

RepoDb.PostgreSql.BulkOperations The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library that contains the official Bulk Operations of RepoDb for PostgreSQL.

GitHub repositories (6)

Showing the top 5 popular GitHub repositories that depend on RepoDb:

Repository Stars
mikependon/RepoDB
A hybrid ORM library for .NET.
itlibrium/DDD-starter-dotnet
Sample implementation and comparison of various approaches to building DDD applications. Useful as a baseline to quickly start a DDD dot net project.
TortugaResearch/DotNet-ORM-Cookbook
This repository is meant to show how to perform common tasks using C# with variety of ORMs.
bcssov/IronyModManager
Mod Manager for Paradox Games. Official Discord: https://discord.gg/t9JmY8KFrV
FransBouma/RawDataAccessBencher
Bench code which tests entity materialization speed of various .NET data access / ORM implementations
Version Downloads Last updated
1.13.2-alpha1 633 2/26/2024
1.13.1 135,820 3/16/2023
1.13.0 71,052 11/2/2022
1.12.10 305,482 2/18/2022
1.12.9 30,657 9/27/2021
1.12.8 82,256 9/23/2021
1.12.7 169,355 2/6/2021
1.12.6 4,616 1/13/2021
1.12.5 9,693 12/30/2020
1.12.4 68,585 10/3/2020
1.12.3 3,088 9/29/2020
1.12.2 1,326 9/28/2020
1.12.1 1,387 9/25/2020
1.12.0 4,427 9/24/2020
1.3.2-alpha1 295 2/26/2024

[UPDATES]
- EntityNotBulkInsertableException supported when calling BulkInsert with non SqlConnection connection object
- BulkInsert optimization - ordering the columns based on the DB Table ordering (not on DataEntity properties ordering)
- Initial implementation for complex query (QueryGroup)
- Added QueryGroup (IQueryGroup)
- Added Conjuctions for QueryGroup(s)
- Added OrQueryGroup
- Added AndQueryGroup
- Supported IFieldValue interface for QueryField - to support the QueryGroup parameterized approach
- Renamed QueryField.Value to QueryField.Parameter (with Name and Value properties) and change its Type from object to IParameter
- In QueryFields, if the Operation is '=' and the parameter Value is equals to NULL then we compose ([Field] IS NULL) string statement
- Solves the problem of: WHERE (SomeDate IS NULL)
- We also did the same thing for != (SomeDate IS NOT NULL)
- Used the '<>' instead of '!=' in InEquality comparer
- Supported EventNotifier (Tracing and Debugging purposes).
- You can listen on the following events to see the Statement and Object passed before DB executions
- BeforeQueryExecution
- AfterQueryExecution
- BeforeUpdateExecution
- AfterUpdateExecution
- BeforeDeleteExecution
- AfterDeleteExecution
- BeforeMergeExecution
- AfterMergeExecution
- BeforeInsertExecution
- AfterInsertExecution
- BeforeBulkInsertExecution
- AfterBulkInsertExecution
- BeforeExecuteNonQueryExecution
- AfterExecuteNonQueryExecution
- BeforeExecuteReaderExecution
- AfterExecuteReaderExecution
- BeforeExecuteScalarExecution
- AfterExecuteScalarExecution
- CancelledExecution