Skip to content

Commit

Permalink
Merge pull request #147 from linq2db/master
Browse files Browse the repository at this point in the history
Release 5.3.0
  • Loading branch information
sdanyliv authored Jun 3, 2021
2 parents 8c20620 + 9f9dd36 commit 24f688d
Show file tree
Hide file tree
Showing 25 changed files with 401 additions and 86 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>5.2.1</Version>
<Version>5.3.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.1">
<dependency id="Microsoft.EntityFrameworkCore.Relational" version="5.0.2" />
<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 @@ -158,12 +158,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;
});

var storeObjectId = GetStoreObjectIdentifier(et);

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="5.0.2" />
<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.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
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
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Linq;
using FluentAssertions;
Expand All @@ -20,7 +20,7 @@ public IdTests()
.ReplaceService<IValueConverterSelector, IdValueConverterSelector>()
.UseLoggerFactory(TestUtils.LoggerFactory)
.EnableSensitiveDataLogging()
.UseNpgsql("Server=DBHost;Port=5432;Database=TestData;User Id=postgres;Password=TestPassword;Pooling=true;MinPoolSize=10;MaxPoolSize=100;")
.UseNpgsql("Server=DBHost;Port=5432;Database=IdTests;User Id=postgres;Password=TestPassword;Pooling=true;MinPoolSize=10;MaxPoolSize=100;")
.Options);
_efContext.Database.EnsureDeleted();
_efContext.Database.EnsureCreated();
Expand Down
26 changes: 26 additions & 0 deletions Tests/LinqToDB.EntityFrameworkCore.SQLite.Tests/ForMappingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using LinqToDB.EntityFrameworkCore.BaseTests;
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
using LinqToDB.EntityFrameworkCore.SQLite.Tests.Models.ForMapping;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;

namespace LinqToDB.EntityFrameworkCore.SQLite.Tests
{
[TestFixture]
public class ForMappingTests : ForMappingTestsBase
{
public override ForMappingContextBase CreateContext()
{
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
optionsBuilder.UseSqlite("DataSource=:memory:");
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);

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

ctx.Database.EnsureCreated();

return ctx;
}
}
}
Loading

0 comments on commit 24f688d

Please sign in to comment.