Dapper.Extension.AspNetCore
1.0.1
See the version list below for details.
dotnet add package Dapper.Extension.AspNetCore --version 1.0.1
NuGet\Install-Package Dapper.Extension.AspNetCore -Version 1.0.1
<PackageReference Include="Dapper.Extension.AspNetCore" Version="1.0.1" />
paket add Dapper.Extension.AspNetCore --version 1.0.1
#r "nuget: Dapper.Extension.AspNetCore, 1.0.1"
// Install Dapper.Extension.AspNetCore as a Cake Addin #addin nuget:?package=Dapper.Extension.AspNetCore&version=1.0.1 // Install Dapper.Extension.AspNetCore as a Cake Tool #tool nuget:?package=Dapper.Extension.AspNetCore&version=1.0.1
Dapper.Extension.AspNetCore
Dapper extensions for ASP.NET Core.
Packages & Status
Packages | NuGet |
---|---|
Dapper.Extension.AspNetCore | |
Dapper.Extension.AspNetCore.MySql |
Features
For these extensions to work, the entity in question MUST have a
key property. Dapper will automatically use a property named "id
"
(case-insensitive) as the key property, if one is present.
public class WeatherForecast
{
public int Id { get; set; } // Works by convention
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }
}
If the entity doesn't follow this convention, decorate
a specific property with a [Key]
or [ExplicitKey]
attribute.
[Table("WeatherForecast")]
public class WeatherForecast
{
[Key]
public int ForecastId { get; set; }
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
[Write(false)]
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
All Attributes
[Table("Tablename")]
- use another table name instead of the name of the class[Key]
- this property represents a database-generated identity/key[ExplicitKey]
- this property represents an explicit identity/key which is not automatically generated by the database[Write(true/false)]
- this property is (not) writable[Computed]
- this property is computed and should not be part of updates
Query
methods
Get one specific entity
var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast;");
var result = _dapper.QuerySingleOrDefault<WeatherForecast>("select * from WeatherForecast;");
var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast where Id = @id;",new { id = 1 });
var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast where Id = @id;",new { id = 1 });
or a list of all entities
in the table
var result = _dapper.Query<WeatherForecast>("select * from WeatherForecast;");
or a list of all entities by page
in the table.
var result = _dapper.QueryPlainPage<WeatherForecast>("select * from WeatherForecast limit @Skip,@Take;", pageIndex, pageSize);
var result = _dapper.QueryPage<WeatherForecast>("select count(*) from WeatherForecast;", "select * from WeatherForecast limit @Take OFFSET @Skip;", pageIndex, pageSize);
Get
methods
Get one specific entity based on id
var forecast = _dapper.Get<WeatherForecast>(1);
or a list of all entities in the table.
var forecasts = _dapper.GetAll<WeatherForecast>();
Insert
methods
Insert one entity
_dapper.Insert(new WeatherForecast { Name = "Volvo" });
or a list of entities.
_dapper.Insert(forecasts);
Update
methods
Update one specific entity
_dapper.Update(new WeatherForecast() { Id = 1, Name = "Saab" });
or update a list of entities.
_dapper.Update(forecasts);
Delete
methods
Delete an entity by the specified [Key]
property
_dapper.Delete(new WeatherForecast() { Id = 1 });
a list of entities
_dapper.Delete(forecasts);
or ALL entities in the table.
_dapper.DeleteAll<WeatherForecast>();
Dapper.Extension.AspNetCore.MySql
Dapper extensions of MySql Database for ASP.NET Core.
Installation
For more information, please view our Getting Started guide.
https://www.nuget.org/packages/Dapper.Extension.AspNetCore.MySql
PM> Install-Package Dapper.Extension.AspNetCore.MySql
Configure
- Add the
AddDapperForMySql()
call into yourConfigureServices
method of theStartup
class.
using Microsoft.Extensions.DependencyInjection;
public class Startup
{
//...
public void ConfigureServices(IServiceCollection services)
{
//...
// services.AddDapperForMySql("server=localhost;uid=root;pwd=123456;database=demo;charset=utf8mb4;");
services.AddDapperForMySql(options =>
{
options.ConnectionString = Configuration.GetConnectionString("MySql");
});
//...
}
}
appsettings.{env}.json
:
{
"ConnectionStrings": {
"MySql": "server=localhost;uid=root;pwd=123456;database=demo;charset=utf8mb4;"
}
}
Usage
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly IDapper _dapper;
public WeatherForecastController(IDapper dapper)
{
_dapper = dapper;
}
[HttpGet]
public IActionResult GetByPage(int pageIndex = 1, int pageSize = 20)
{
// var result = _dapper.Query<WeatherForecast>("select * from WeatherForecast;");
// var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast;");
// var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast where Id = @id;",new { id = 1 });
// var result = _dapper.QuerySingleOrDefault<WeatherForecast>("select * from WeatherForecast;");
// var result = _dapper.QueryFirstOrDefault<WeatherForecast>("select * from WeatherForecast where Id = @id;",new { id = 1 });
// var result = _dapper.QueryPlainPage<WeatherForecast>("select * from WeatherForecast limit @Skip,@Take;", pageIndex, pageSize);
var result = _dapper.QueryPage<WeatherForecast>("select count(*) from WeatherForecast;", "select * from WeatherForecast limit @Take OFFSET @Skip;", pageIndex, pageSize);
return Ok(result);
}
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var result = _dapper.Get<WeatherForecast>(id);
return Ok(result);
}
[HttpPost]
public void Post()
{
var rng = new Random();
_dapper.Insert(new WeatherForecast
{
Date = DateTime.Now.AddDays(rng.Next(Summaries.Length)),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
});
}
[HttpDelete("id")]
public void Delete(int id)
{
_dapper.Delete(new WeatherForecast() { Id = id });
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 is compatible. netcoreapp3.0 is compatible. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 2.0
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 2.0.2)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.0.2)
-
.NETCoreApp 2.1
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 2.1.1)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.1.1)
-
.NETCoreApp 2.2
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.2.0)
-
.NETCoreApp 3.0
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 3.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 3.0.0)
-
.NETCoreApp 3.1
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 3.1.10)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 3.1.10)
-
.NETStandard 2.0
- Dapper (>= 2.0.78)
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.2.0)
- System.Reflection.Emit (>= 4.7.0)
-
.NETStandard 2.1
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 3.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 3.0.0)
-
net5.0
- Dapper (>= 2.0.78)
- Microsoft.Extensions.Configuration.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Dapper.Extension.AspNetCore:
Package | Downloads |
---|---|
Dapper.Extension.AspNetCore.MySql
Dapper extensions of MySql Database for ASP.NET Core |
GitHub repositories
This package is not used by any popular GitHub repositories.
Override Dapper.Extension