Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/linq2db/linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
https://github.com/linq2db/linq2db.EntityFrameworkCore

dotnet-core entity-framework entity-framework-core entityframeworkcore linq linq2db orm sql

Last synced: about 2 months ago
JSON representation

Bring power of Linq To DB to Entity Framework Core projects

Awesome Lists containing this project

README

        

# linq2db.EntityFrameworkCore

`linq2db.EntityFrameworkCore` is an integration of `LINQ To DB` with existing EntityFrameworkCore projects. It was inspired by [this issue](https://github.com/aspnet/EntityFrameworkCore/issues/11657) in EF.Core repository.

## Build status

[![Azure DevOps builds](https://img.shields.io/azure-devops/build/linq2db/0dcc414b-ea54-451e-a54f-d63f05367c4b/7/master?label=master)](https://dev.azure.com/linq2db/linq2db/_build?definitionId=7)
[![Azure DevOps builds](https://img.shields.io/azure-devops/build/linq2db/0dcc414b-ea54-451e-a54f-d63f05367c4b/7?label=latest)](https://dev.azure.com/linq2db/linq2db/_build?definitionId=7)
[![Azure DevOps builds](https://img.shields.io/azure-devops/build/linq2db/0dcc414b-ea54-451e-a54f-d63f05367c4b/7/version1?label=efcore%202%20master)](https://dev.azure.com/linq2db/linq2db/_build?definitionId=7)

## Feeds

* NuGet [![NuGet](https://img.shields.io/nuget/vpre/linq2db.EntityFrameworkCore.svg)](https://www.nuget.org/packages/linq2db.EntityFrameworkCore)
* Azure Artifacts [![MyGet](https://img.shields.io/badge/azure-download-yellowgreen)](https://dev.azure.com/linq2db/linq2db/_packaging?_a=package&feed=linq2db&view=versions&package=linq2db.EntityFrameworkCore&protocolType=NuGet) ([feed]( https://pkgs.dev.azure.com/linq2db/linq2db/_packaging/linq2db/nuget/v3/index.json))

# Unique features
* Fast Eager Loading (incomparable faster on massive `Include` query)
* Global Query Filters optimization
* Better SQL optimization
* [Use CTE in LINQ queries](https://linq2db.github.io/articles/sql/CTE.html)
* [MERGE statement support](https://linq2db.github.io/articles/sql/merge/Merge-API-Description.html)
* Table hints
* [Full Window functions support](https://linq2db.github.io/articles/sql/Window-Functions-(Analytic-Functions).html)
* Fast [BulkCopy](https://linq2db.github.io/articles/sql/Bulk-Copy.html) of millions records
* Native SQL operations for updating, deleting, inserting records via LINQ query
* Temporary Tables support
* Cross Database/Linked Server queries.
* Full Text Search extensions
* A lot of extensions to cover ANSI SQL
# How to use

In your code you need to initialize integration using following call:

```cs
LinqToDBForEFTools.Initialize();
```

After that you can just call DbContext and IQueryable extension methods, provided by `LINQ To DB`.

You can also register additional options (like interceptors) for LinqToDB during EF context registration, here is an example:

```cs
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlite();
optionsBuilder.UseLinqToDB(builder =>
{
// add custom command interceptor
builder.AddInterceptor(new MyCommandInterceptor());
// add additional mappings
builder.AddMappingSchema(myCustomMappings);
// configure SQL Server dialect explicitly
builder.AddCustomOptions(o => o.UseSqlServer(SqlServerVersion.v2022));
});
```

There are many extensions for CRUD Operations missing in vanilla EF ([watch our video](https://www.youtube.com/watch?v=m--oX73EGeQ)):

```cs
// fast insert big recordsets
ctx.BulkCopy(new BulkCopyOptions {...}, items);

// query for retrieving products that do not have duplicates by Name
var query =
from p in ctx.Products
from op in ctx.Products.LeftJoin(op => op.ProductID != p.ProductID && op.Name == p.Name)
where Sql.ToNullable(op.ProductID) == null
select p;

// insert these records into the same or another table
query.Insert(ctx.Products.ToLinqToDBTable(), s => new Product { Name = s.Name ... });

// update these records by changing name based on previous value
query.Update(prev => new Product { Name = "U_" + prev.Name ... });

// delete records that matched by query
query.Delete();
```

Some extensions require LINQ To DB `ITable` interface, which could be acquired from `DbSet` using `ToLinqToDBTable()` extension method.

For `ITable` interface LINQ To DB provides several extensions that may be useful for complex databases and custom queries:

```cs
table = table.TableName("NewTableName"); // change table name in query
table = table.DatabaseName("OtherDatabase"); // change database name, useful for cross database queries.
table = table.OwnerName("OtherOwner"); // change owner.

// inserting into other existing table Products2
query.Insert(ctx.Products.ToLinqToDBTable().TableName("Products2"), s => new Product { Name = s.Name ... });
```

It is not required to work directly with `LINQ To DB` `DataConnection` class but there are several ways to do that. `LINQ To DB` will try to reuse your configuration and select appropriate data provider:

```cs
// uing DbContext
using (var dc = ctx.CreateLinqToDBConnection())
{
// linq queries using linq2db extensions
}

// using DbContextOptions
using (var dc = options.CreateLinqToDBConnection())
{
// linq queries using linq2db extensions
}
```

You can use all `LINQ To DB` extension functions in your EF linq queries. Just ensure you have called `ToLinqToDB()` function before materializing objects for synchronous methods.

Since EF Core have defined it's own asynchronous methods, we have to duplicate them to resolve naming collisions.
Async methods have the same name but with `LinqToDB` suffix. E.g. `ToListAsyncLinqToDB()`, `SumAsyncLinqToDB()`, ect. The same methods are added whe you need `EF Core` query processing but there is collision with `LINQ To DB` and they have extensions with `EF` suffix - `ToListAsyncEF()`, `SumAsyncEF()`, ect.

```cs
using (var ctx = CreateAdventureWorksContext())
{
var productsWithModelCount =
from p in ctx.Products
select new
{
// Window Function
Count = Sql.Ext.Count().Over().PartitionBy(p.ProductModelID).ToValue(),
Product = p
};

var neededRecords =
from p in productsWithModelCount
where p.Count.Between(2, 4) // LINQ To DB extension
select new
{
p.Product.Name,
p.Product.Color,
p.Product.Size,
// retrieving value from column dynamically
PhotoFileName = Sql.Property(p.Product, "ThumbnailPhotoFileName")
};

// ensure we have replaced EF context
var items1 = neededRecords.ToLinqToDB().ToArray();

// async version
var items2 = await neededRecords.ToLinqToDB().ToArrayAsync();

// and simple bonus - how to generate SQL
var sql = neededRecords.ToLinqToDB().ToString();
}
```

Also check [existing tests](https://github.com/linq2db/linq2db.EntityFrameworkCore/blob/master/Tests/LinqToDB.EntityFrameworkCore.Tests/ToolsTests.cs) in test project for some examples.

# Why should I want to use it?

There are many reasons. Some of them:

- you want to use advanced SQL functionality, not supported or poorly supported by EntityFrameworkCore like BulkCopy support, SQL MERGE operations, convinient DML (Insert/Delete/Update) operations and many-many-many other features LINQ To DB provides, but you need change tracking functionality that EntityFramework provides.
- you want to migrate to LINQ To DB, but need to do it step-by-step.
- just because LINQ To DB is cool.

# Current status

Below is a list of providers, that should work right now:

- SQL Server
- MySQL (including Devart and Pomelo providers)
- PostgreSQL (Both npgsql and Devart providers)
- SQLite (including Devart provider)
- Firebird
- DB2 LUW
- Oracle
- SQL Server CE

# Known limitations
- No Lazy loading
- No way to work with in-memory database
- No TPT (table per type) support
- No many-to-many support

# Help! It doesn't work!

If you encounter any issue with this library, first check issues to see if it was already reported and if not, feel free to report new issue.