Bring power of Linq To DB to Entity Framework Core projects
linq2db.EntityFrameworkCoreis an integration of
LINQ To DBwith existing EntityFrameworkCore projects. It was inspired by this issue in EF.Core repository.
Includequery)
In your code you need to initialize integration using following call:
LinqToDBForEFTools.Initialize();
After that you can just call DbContext and IQueryable extension methods, provided by
LINQ To DB.
There are many extensions for CRUD Operations missing in vanilla EF (watch our video):
// 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
ITableinterface, which could be acquired from
DbSetusing
ToLinqToDBTable()extension method.
For
ITableinterface LINQ To DB provides several extensions that may be useful for complex databases and custom queries:
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
DataConnectionclass but there are several ways to do that.
LINQ To DBwill try to reuse your configuration and select appropriate data provider:
// 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 DBextension 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
LinqToDBsuffix. E.g.
ToListAsyncLinqToDB(),
SumAsyncLinqToDB(), ect. The same methods are added whe you need
EF Corequery processing but there is collision with
LINQ To DBand they have extensions with
EFsuffix -
ToListAsyncEF(),
SumAsyncEF(), ect.
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<string>(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 in test project for some examples.
There are many reasons. Some of them:
Below is a list of providers, that should work right now:
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.