Skip to content

Commit

Permalink
Merge pull request #148 from linq2db/version3
Browse files Browse the repository at this point in the history
Release 3.11.0
  • Loading branch information
sdanyliv authored Jun 3, 2021
2 parents e7078cf + 867de0d commit c7f2c71
Show file tree
Hide file tree
Showing 44 changed files with 1,050 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Build/linq2db.Default.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>3.10.1</Version>
<Version>3.11.0</Version>

<Authors>Svyatoslav Danyliv, Igor Tkachev, Dmitry Lukashenko, Ilya Chudin</Authors>
<Product>Linq to DB</Product>
Expand Down
2 changes: 1 addition & 1 deletion NuGet/linq2db.EntityFrameworkCore.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependencies>
<group targetFramework=".NETStandard2.0">
<dependency id="Microsoft.EntityFrameworkCore.Relational" version="3.1.10" />
<dependency id="linq2db" version="3.3.0" />
<dependency id="linq2db" version="3.4.0" />
</group>
</dependencies>
</metadata>
Expand Down
25 changes: 19 additions & 6 deletions Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,25 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
}

var isIdentity = prop.GetAnnotations()
.Any(a => a.Name.EndsWith(":ValueGenerationStrategy") && a.Value?.ToString() == "IdentityColumn");

if (!isIdentity && isPrimaryKey)
{
isIdentity = prop.ValueGenerated == ValueGenerated.OnAdd;
}
.Any(a =>
{
if (a.Name.EndsWith(":ValueGenerationStrategy"))
return a.Value?.ToString().Contains("Identity") == true;

if (a.Name.EndsWith(":Autoincrement"))
return a.Value is bool b && b;

// for postgres
if (a.Name == "Relational:DefaultValueSql")
{
if (a.Value is string str)
{
return str.ToLower().Contains("nextval");
}
}

return false;
});

return new T[]{(T)(Attribute) new ColumnAttribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static async Task<BulkCopyRowsCopied> BulkCopyAsync<T>(
[LinqTunnel]
[Pure]
public static IValueInsertable<T> Into<T>(this DbContext context, ITable<T> target)
where T: notnull
where T: notnull
{
if (context == null) throw new ArgumentNullException(nameof(context));
if (target == null) throw new ArgumentNullException(nameof(target));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Caching.Memory;

namespace LinqToDB.EntityFrameworkCore
{
Expand Down Expand Up @@ -84,10 +85,10 @@ public override int GetHashCode()
#endregion
}

readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new ConcurrentDictionary<ProviderKey, IDataProvider>();
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new();

private readonly MemoryCache _schemaCache = new MemoryCache(
new MemoryCacheOptions
private readonly MemoryCache _schemaCache = new(
new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions
{
ExpirationScanFrequency = TimeSpan.FromHours(1.0)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="linq2db" Version="3.3.0" />
<PackageReference Include="linq2db" Version="3.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.11" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using LinqToDB.Data;
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using NUnit.Framework;

namespace LinqToDB.EntityFrameworkCore.BaseTests
{
public abstract class ForMappingTestsBase : TestsBase
{
public abstract ForMappingContextBase CreateContext();

[Test]
public virtual void TestIdentityMapping()
{
using var context = CreateContext();
using var connection = context.CreateLinqToDbConnection();

var ed = connection.MappingSchema.GetEntityDescriptor(typeof(WithIdentity));
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();

pk.IsIdentity.Should().BeTrue();
}

[Test]
public virtual void TestNoIdentityMapping()
{
using var context = CreateContext();
using var connection = context.CreateLinqToDbConnection();

var ed = connection.MappingSchema.GetEntityDescriptor(typeof(NoIdentity));
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();

pk.IsIdentity.Should().BeFalse();
}

[Test]
public virtual void TestTableCreation()
{
using var context = CreateContext();
using var connection = context.CreateLinqToDbConnection();

using var t1 = connection.CreateTempTable<WithIdentity>();
using var t2 = connection.CreateTempTable<NoIdentity>();
}


[Test]
public virtual void TestBulkCopyNoIdentity()
{
using var context = CreateContext();
using var connection = context.CreateLinqToDbConnection();

using var t = connection.CreateTempTable<NoIdentity>();

var items = new List<NoIdentity>()
{
new() {Id = Guid.NewGuid(), Name = "John Doe"},
new() {Id = Guid.NewGuid(), Name = "Jane Doe"}
};

t.BulkCopy(items);


items.Should().BeEquivalentTo(t);
}

[Test]
public virtual void TestBulkCopyWithIdentity()
{
using var context = CreateContext();
using var connection = context.CreateLinqToDbConnection();

using var t = connection.CreateTempTable<WithIdentity>();

var items = new List<WithIdentity>()
{
new() {Id = 1, Name = "John Doe"},
new() {Id = 2, Name = "Jane Doe"}
};

t.BulkCopy(items);


t.Should().HaveCount(items.Count);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="linq2db.Tools" Version="3.2.3" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.11" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;

namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
{
public abstract class ForMappingContextBase : DbContext
{
protected ForMappingContextBase(DbContextOptions options) : base(options)
{
}

public DbSet<WithIdentity> WithIdentity { get; set; } = null!;
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
{
public class NoIdentity
{
public Guid Id { get; set; }
public string Name { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
{
public class WithIdentity
{
public int Id { get; set; }
public string Name { get; set; } = null!;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using LinqToDB.Data;
using LinqToDB.EntityFrameworkCore.BaseTests;
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.Models.ForMapping;
using Microsoft.EntityFrameworkCore;

namespace LinqToDB.EntityFrameworkCore.PomeloMySql.Tests
{
public class ForMappingTests : ForMappingTestsBase
{
public override ForMappingContextBase CreateContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
optionsBuilder.UseMySql("Server=DBHost;Port=3306;Database=TestData;Uid=TestUser;Pwd=TestPassword;charset=utf8;");
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);

var options = optionsBuilder.Options;
var ctx = new ForMappingContext(options);

//ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();

return ctx;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
<ProjectReference Include="..\LinqToDB.EntityFrameworkCore.BaseTests\LinqToDB.EntityFrameworkCore.BaseTests.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using Microsoft.EntityFrameworkCore;

namespace LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.Models.ForMapping
{
public class ForMappingContext : ForMappingContextBase
{

public ForMappingContext(DbContextOptions options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WithIdentity>(b =>
{
b.HasKey(e => e.Id);

b.Property(e => e.Id)
.UseMySqlIdentityColumn();
});

modelBuilder.Entity<NoIdentity>(b =>
{
b.HasKey(e => e.Id);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using LinqToDB.Data;
using LinqToDB.EntityFrameworkCore.BaseTests;
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using LinqToDB.EntityFrameworkCore.PostgreSQL.Tests.Models.ForMapping;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;

namespace LinqToDB.EntityFrameworkCore.PostgreSQL.Tests
{
[TestFixture]
public class ForMappingTests : ForMappingTestsBase
{
public override ForMappingContextBase CreateContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
optionsBuilder.UseNpgsql("Server=DBHost;Port=5432;Database=ForMapping;User Id=postgres;Password=TestPassword;Pooling=true;MinPoolSize=10;MaxPoolSize=100;");
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);

var options = optionsBuilder.Options;
var ctx = new ForMappingContext(options);

//ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();

return ctx;
}

//Disabled, we cannot create such identity table.
public override void TestBulkCopyWithIdentity()
{
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using Microsoft.EntityFrameworkCore;

namespace LinqToDB.EntityFrameworkCore.PostgreSQL.Tests.Models.ForMapping
{
public class ForMappingContext : ForMappingContextBase
{

public ForMappingContext(DbContextOptions options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WithIdentity>(b =>
{
b.HasKey(e => e.Id);

b.Property(e => e.Id)
.UseIdentityAlwaysColumn();
});

modelBuilder.Entity<NoIdentity>(b =>
{
b.HasKey(e => e.Id);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasNoKey();
entity.ToView("EventsView", "views");
});

modelBuilder.Entity<EntityWithArrays>(entity =>
{
});
}

public virtual DbSet<Event> Events { get; set; } = null!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ private NpgSqlEnititesContext CreateNpgSqlEntitiesContext()
return ctx;
}


[Test]
public void TestFunctionsMapping()
{
using (var db = CreateNpgSqlEntitiesContext())
{
var date = DateTime.UtcNow;
var date = DateTime.Now;

var query = db.Events.Where(e =>
e.Duration.Contains(date) || e.Duration.LowerBound == date || e.Duration.UpperBound == date ||
Expand Down
Loading

0 comments on commit c7f2c71

Please sign in to comment.