Skip to content

Commit

Permalink
backport linq2db 5 migration from v7 (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaceWindu authored Feb 26, 2023
1 parent 525c1c7 commit 37fc750
Show file tree
Hide file tree
Showing 35 changed files with 930 additions and 940 deletions.
4 changes: 2 additions & 2 deletions Build/linq2db.Default.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project>
<PropertyGroup>
<Version>2.9.0</Version>
<Version>2.10.0</Version>

<Authors>Svyatoslav Danyliv, Igor Tkachev, Dmitry Lukashenko, Ilya Chudin</Authors>
<Product>Linq to DB</Product>
<Company>linq2db.net</Company>
<Copyright>2002-2022 linq2db.net</Copyright>
<Copyright>2002-2023 linq2db.net</Copyright>
<RepositoryUrl>https://github.com/linq2db/linq2db.EntityFrameworkCore</RepositoryUrl>
<RepositoryType>git</RepositoryType>

Expand Down
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageVersion Include="NUnit" Version="3.13.3" />
<PackageVersion Include="FluentAssertions" Version="6.8.0" />
<PackageVersion Include="FluentAssertions" Version="6.10.0" />

<PackageVersion Include="linq2db" Version="4.4.0" />
<PackageVersion Include="linq2db.Tools" Version="4.4.0" />
<PackageVersion Include="linq2db" Version="5.0.0" />
<PackageVersion Include="linq2db.Tools" Version="5.0.0" />

<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
Expand Down
6 changes: 3 additions & 3 deletions NuGet/linq2db.EntityFrameworkCore.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Linq to DB (linq2db) extensions for Entity Framework Core 2.x</title>
<authors>Igor Tkachev, Ilya Chudin, Svyatoslav Danyliv, Dmitry Lukashenko</authors>
<owners>Igor Tkachev, Ilya Chudin, Svyatoslav Danyliv, Dmitry Lukashenko</owners>
<copyright>Copyright © 2020-2022 Igor Tkachev, Ilya Chudin, Svyatoslav Danyliv, Dmitry Lukashenko</copyright>
<copyright>Copyright © 2020-2023 Igor Tkachev, Ilya Chudin, Svyatoslav Danyliv, Dmitry Lukashenko</copyright>
<description>Allows to execute Linq to DB (linq2db) queries in Entity Framework Core DbContext.</description>
<summary />
<tags>linq linq2db LinqToDB ORM database entity-framework-core EntityFrameworkCore EFCore DB SQL SqlServer SqlCe SqlServerCe MySql Firebird SQLite Oracle ODP PostgreSQL DB2</tags>
Expand All @@ -14,8 +14,8 @@
<projectUrl>https://github.com/linq2db/linq2db.EntityFrameworkCore</projectUrl>
<license type="file">MIT-LICENSE.txt</license>
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="linq2db" version="4.4.0" />
<group targetFramework="netstandard2.0">
<dependency id="linq2db" version="5.0.0" />
<dependency id="Microsoft.EntityFrameworkCore.Relational" version="2.2.6" />
<dependency id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" />
<dependency id="System.Interactive.Async" version="3.2.0" />
Expand Down
84 changes: 47 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ You can also register additional options (like interceptors) for LinqToDB during
```cs
var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
optionsBuilder.UseSqlite();
optionsBuilder.UseLinqToDb(builder =>
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));
// due to bug in linq2db 5.0.0, use overload with connection string
// will be fixed in next linq2db release
builder.AddCustomOptions(o => o.UseSqlServer("unused", SqlServerVersion.v2022));
});
```

Expand All @@ -58,10 +66,10 @@ 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;
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 ... });
Expand Down Expand Up @@ -90,13 +98,13 @@ It is not required to work directly with `LINQ To DB` `DataConnection` class but

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

// using DbContextOptions
using (var dc = options.CreateLinqToDbConnection())
using (var dc = options.CreateLinqToDBConnection())
{
// linq queries using linq2db extensions
}
Expand All @@ -110,35 +118,35 @@ Async methods have the same name but with `LinqToDB` suffix. E.g. `ToListAsyncLi
```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<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();
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();
}
```

Expand All @@ -165,9 +173,11 @@ Below is a list of providers, that should work right now:
- Oracle
- SQL Server CE

# Know limitations
# 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!

Expand Down
Loading

0 comments on commit 37fc750

Please sign in to comment.