linq2db.AspNet
5.4.1
Prefix Reserved
dotnet add package linq2db.AspNet --version 5.4.1
NuGet\Install-Package linq2db.AspNet -Version 5.4.1
<PackageReference Include="linq2db.AspNet" Version="5.4.1" />
paket add linq2db.AspNet --version 5.4.1
#r "nuget: linq2db.AspNet, 5.4.1"
// Install linq2db.AspNet as a Cake Addin #addin nuget:?package=linq2db.AspNet&version=5.4.1 // Install linq2db.AspNet as a Cake Tool #tool nuget:?package=linq2db.AspNet&version=5.4.1
LINQ to DB ASP.NET Core
Main article.
Setup Walkthrough
Create a new project
First thing we're going to do is create a new ASP.NET Core application using the dotnet CLI
dotnet new webapp -o gettingStartedLinqToDBAspNet
Install LINQ To DB
We can now use the CLI to install LINQ To DB and database provider (SQLite in this walkthrough)
dotnet add package linq2db.AspNet
dotnet add package System.Data.SQLite.Core
Custom Data Connection
We're going to create a custom data connection to use to access LINQ To DB, create a class like this:
using LinqToDB.Configuration;
using LinqToDB.Data;
public class AppDataConnection: DataConnection
{
public AppDataConnection(DataOptions<AppDataConnection> options)
:base(options.Options)
{
}
}
[!TIP]
Note here ourAppDataConnection
inherits fromLinqToDB.Data.DataConnection
which is the base class for theLinq To DB
connection.[!TIP]
a public constructor that acceptsDataOptions<AppDataConnection>
and passes the options on to the base constructor is required.
Add Connection String
For this example we're going to use SQLite in memory mode, for production you'll want to use something else, but it's pretty easy to change.
First you want to add the connection string to appsettings.Development.json
, something like this:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
//add this
"ConnectionStrings": {
"Default": ":memory:" //<-- connection string, used in the next step
}
}
Configure Dependency injection
inside Startup.cs
you want register the data connection like this:
public class Startup
{
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//...
//using LinqToDB.AspNet
services.AddLinqToDBContext<AppDataConnection>((provider, options)
=> options
//will configure the AppDataConnection to use
//sqite with the provided connection string
//there are methods for each supported database
.UseSQLite(Configuration.GetConnectionString("Default"))
//default logging will log everything using the ILoggerFactory configured in the provider
.UseDefaultLogging(provider));
//...
}
}
[!TIP]
There's plenty of other configuration options available, if you are familiar withLINQ To DB
already, you can convert your existing application over to use the newDataOptions
class as every configuration method is supported[!TIP]
UseAddLinqToDBContext<TContext, TContextImplementation>
if you would like to resolve an interface or base class instead of the concrete class in your controllers
By default this will configure the service provider to create a new AppDataConnection
for each HTTP Request, and will dispose of it once the request is finished. This can be configured with the last parameter to AddLinqToDBContext(... ServiceLifetime lifetime)
, more information about lifetimes here
Simple Entity Configuration
Let's create this simple entity in our project
using System;
using LinqToDB.Mapping;
public class Person
{
[PrimaryKey]
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }
public DateTime Birthday { get; set; }
}
Add table property to the data connection
Open up our AppDataConnection
and add this property
public class AppDataConnection: DataConnection
{
//...
public ITable<Person> People => GetTable<Person>();
//...
}
Now we can inject our data connection into a controller and query and insert/update/delete using the ITable<Person>
interface.
[!TIP] side note, since we don't have anything to create the actual database, we need to add this code into the configure method in
Startup.cs
public class Startup { //... public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //... using (var scope = app.ApplicationServices.CreateScope()) { var dataConnection = scope.ServiceProvider.GetService<AppDataConnection>(); dataConnection.CreateTable<Person>(); } //... } } //...
Inject the connection into a controller
In order to actually access the database we're going to want to use it from a controller, here's a sample controller to get you started with a few examples.
public class PeopleController : Controller
{
private readonly AppDataConnection _connection;
public PeopleController(AppDataConnection connection)
{
_connection = connection;
}
[HttpGet]
public Task<Person[]> ListPeople()
{
return _connection.People.ToArrayAsync();
}
[HttpGet("{id}")]
public Task<Person?> GetPerson(Guid id)
{
return _connection.People.SingleOrDefaultAsync(person => person.Id == id);
}
[HttpDelete("{id}")]
public Task<int> DeletePerson(Guid id)
{
return _connection.People.Where(person => person.Id == id).DeleteAsync();
}
[HttpPatch]
public Task<int> UpdatePerson(Person person)
{
return _connection.UpdateAsync(person);
}
[HttpPatch("{id}/new-name")]
public Task<int> UpdatePersonName(Guid id, string newName)
{
return _connection.People.Where(person => person.Id == id)
.Set(person => person.Name, newName)
.UpdateAsync();
}
[HttpPut]
public Task<int> InsertPerson(Person person)
{
return _connection.InsertAsync(person);
}
}
Quick start for people already familiar with LINQ To DB
LINQ To DB now has support for ASP.NET Dependency injection. Here's a simple example of how to add it to dependency injection
public class Startup
{
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//...
//using LinqToDB.AspNet
services.AddLinqToDBContext<AppDataConnection>((provider, options)
=> options
//will configure the AppDataConnection to use
//SqlServer with the provided connection string
//there are methods for each supported database
.UseSqlServer(Configuration.GetConnectionString("Default"))
//default logging will log everything using
//an ILoggerFactory configured in the provider
.UseDefaultLogging(provider));
//...
}
}
We've done our best job to allow any existing use case to be migrated to using the new configuration options, please create an issue if something isn't supported. There are also some methods to setup tracing and mapping schema.
You'll need to update your data connection to accept the new options class too.
public class AppDataConnection: DataConnection
{
public AppDataConnection(DataOptions<AppDataConnection> options)
:base(options.Options)
{
}
}
DataConnection
will use the options passed into the base constructor to setup the connection.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. 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. |
-
.NETFramework 4.5
- linq2db (>= 5.4.1)
- Microsoft.Extensions.DependencyInjection (>= 1.1.1)
- Microsoft.Extensions.Logging.Abstractions (>= 1.1.2)
-
.NETStandard 2.0
- linq2db (>= 5.4.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
-
net6.0
- linq2db (>= 5.4.1)
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on linq2db.AspNet:
Package | Downloads |
---|---|
RCommon.Linq2Db
A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling persistence unit of work mediator distributed messaging event bus CQRS email and more |
|
XulBenet.CRUD.DAL.Linq2Db
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
5.4.1 | 53,738 | 4/7/2024 |
5.4.0 | 37,354 | 2/20/2024 |
5.3.2 | 99,969 | 10/18/2023 |
5.3.1 | 480 | 10/16/2023 |
5.3.0 | 2,850 | 10/12/2023 |
5.2.2 | 53,851 | 6/8/2023 |
5.2.1 | 23,229 | 5/18/2023 |
5.2.0 | 18,162 | 5/4/2023 |
5.1.1 | 4,253 | 3/25/2023 |
5.1.0 | 33,783 | 3/16/2023 |
5.0.0 | 6,234 | 2/23/2023 |
5.0.0-rc.2 | 139 | 2/16/2023 |
5.0.0-rc.1 | 116 | 2/9/2023 |
5.0.0-preview.2 | 139 | 2/2/2023 |
5.0.0-preview.1 | 199 | 1/12/2023 |
4.4.1 | 12,591 | 2/2/2023 |
4.4.0 | 36,476 | 12/15/2022 |
4.3.0 | 153,882 | 10/1/2022 |
4.2.0 | 15,779 | 9/1/2022 |
4.1.1 | 33,162 | 7/7/2022 |
4.1.0 | 44,546 | 6/16/2022 |
4.0.1 | 5,845 | 5/27/2022 |
4.0.0 | 9,371 | 5/19/2022 |
4.0.0-rc.2 | 148 | 5/15/2022 |
4.0.0-rc.1 | 155 | 4/29/2022 |
4.0.0-preview.10 | 86,142 | 11/15/2021 |
4.0.0-preview.9 | 175 | 11/11/2021 |
4.0.0-preview.8 | 240 | 10/22/2021 |
4.0.0-preview.7 | 315 | 10/14/2021 |
4.0.0-preview.6 | 301 | 10/2/2021 |
4.0.0-preview.5 | 309 | 9/10/2021 |
4.0.0-preview.4 | 2,175 | 8/13/2021 |
4.0.0-preview.3 | 781 | 7/9/2021 |
4.0.0-preview.2 | 273 | 7/1/2021 |
4.0.0-preview.1 | 200 | 6/9/2021 |
3.7.0 | 40,660 | 4/7/2022 |
3.6.0 | 140,615 | 11/15/2021 |
3.5.2 | 1,072 | 11/11/2021 |
3.5.1 | 2,290 | 10/22/2021 |
3.5.0 | 1,267 | 10/14/2021 |
3.4.5 | 1,337 | 10/2/2021 |
3.4.4 | 11,503 | 9/10/2021 |
3.4.3 | 7,658 | 8/13/2021 |
3.4.2 | 25,930 | 7/9/2021 |
3.4.1 | 978 | 7/1/2021 |
3.4.0 | 21,373 | 6/3/2021 |
3.3.0 | 396,043 | 3/25/2021 |
3.2.3 | 25,279 | 1/4/2021 |
3.2.2 | 4,120 | 12/26/2020 |
3.2.1 | 5,407 | 12/17/2020 |
3.2.0 | 526 | 12/17/2020 |
3.1.6 | 40,132 | 10/31/2020 |
3.1.5 | 6,066 | 10/15/2020 |
3.1.4 | 2,303 | 10/8/2020 |
3.1.3 | 8,949 | 9/17/2020 |
3.1.2 | 3,761 | 9/3/2020 |
3.1.1 | 1,533 | 8/27/2020 |
3.1.0 | 1,912 | 8/20/2020 |
3.0.1 | 5,042 | 7/13/2020 |
3.0.0 | 9,962 | 7/10/2020 |
3.0.0-rc.2 | 277 | 7/5/2020 |
3.0.0-rc.1 | 363 | 6/17/2020 |
3.0.0-rc.0 | 353 | 5/30/2020 |