Skip to content

Commit

Permalink
Merge pull request #175 from linq2db/version3
Browse files Browse the repository at this point in the history
Release 3.14.0
  • Loading branch information
sdanyliv authored Sep 15, 2021
2 parents 8bc069a + 9a1563d commit 7411783
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 40 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.13.0</Version>
<Version>3.14.0</Version>

<Authors>Svyatoslav Danyliv, Igor Tkachev, Dmitry Lukashenko, Ilya Chudin</Authors>
<Product>Linq to DB</Product>
Expand Down
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<PackageVersion Include="NUnit" Version="3.13.2" />
<PackageVersion Include="FluentAssertions" Version="5.10.3" />

<PackageVersion Include="linq2db" Version="3.4.3" />
<PackageVersion Include="linq2db.Tools" Version="3.4.3" />
<PackageVersion Include="linq2db" Version="3.4.4" />
<PackageVersion Include="linq2db.Tools" Version="3.4.4" />

<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.0.0" />
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
Expand All @@ -15,8 +15,8 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.11" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.11" />

<PackageVersion Include="Microsoft.Extensions.Logging" Version="3.1.11" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="3.1.11" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />

<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="3.2.4" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
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.11" />
<dependency id="linq2db" version="3.4.3" />
<dependency id="linq2db" version="3.4.4" />
</group>
</dependencies>
</metadata>
Expand Down
23 changes: 22 additions & 1 deletion Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute
if (tableAttribute != null)
return new[] { (T)(Attribute)new TableAttribute(tableAttribute.Name) { Schema = tableAttribute.Schema } };
}
else if (_model != null && typeof(T) == typeof(InheritanceMappingAttribute))
{
if (et != null)
{
var derivedEntities = _model.GetEntityTypes().Where(e => e.BaseType == et && e.GetDiscriminatorValue() != null).ToList();

return
derivedEntities.Select(e =>
(T)(Attribute)new InheritanceMappingAttribute
{
Type = e.ClrType,
Code = e.GetDiscriminatorValue()
}
)
.ToArray();
}

}

return Array.Empty<T>();
}
Expand Down Expand Up @@ -186,11 +204,13 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru

if (prop != null)
{
var discriminator = et.GetDiscriminatorProperty();

var isPrimaryKey = prop.IsPrimaryKey();
var primaryKeyOrder = 0;
if (isPrimaryKey)
{
var pk = prop.FindContainingPrimaryKey();
var pk = prop.FindContainingPrimaryKey()!;
primaryKeyOrder = pk.Properties.Select((p, i) => new { p, index = i })
.FirstOrDefault(v => CompareProperty(v.p, memberInfo))?.index ?? 0;
}
Expand Down Expand Up @@ -248,6 +268,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
IsPrimaryKey = isPrimaryKey,
PrimaryKeyOrder = primaryKeyOrder,
IsIdentity = isIdentity,
IsDiscriminator = discriminator == prop
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public override ISqlExpression GetExpression(
{
var mc = (MethodCallExpression) expression;
if (!mc.Method.IsStatic)
knownExpressions.Add(mc.Object);
knownExpressions.Add(mc.Object!);
knownExpressions.AddRange(mc.Arguments);
}
else
{
var me = (MemberExpression) expression;
knownExpressions.Add(me.Expression);
knownExpressions.Add(me.Expression!);
}

var pams = new List<ISqlExpression?>(knownExpressions.Select(_ => (ISqlExpression?) null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
/// </summary>
/// <param name="expression">Query expression.</param>
/// <returns>Query result.</returns>
public object Execute(Expression expression)
public object? Execute(Expression expression)
{
return QueryProvider.Execute(expression);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ TResult IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, Cancell
{
var item = typeof(TResult).GetGenericArguments()[0];
var method = _executeAsyncMethodInfo.MakeGenericMethod(item);
return (TResult) method.Invoke(QueryProvider, new object[] { expression, cancellationToken });
return (TResult) method.Invoke(QueryProvider, new object[] { expression, cancellationToken })!;
}

/// <summary>
Expand Down Expand Up @@ -160,14 +160,15 @@ IEnumerator IEnumerable.GetEnumerator()
/// <returns>Query result as <see cref="IAsyncEnumerable{T}"/>.</returns>
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken)
{
return QueryProvider.ExecuteAsyncEnumerable<T>(Expression, cancellationToken).Result.GetAsyncEnumerator(cancellationToken);
return Task.Run(() => QueryProvider.ExecuteAsyncEnumerable<T>(Expression, cancellationToken),
cancellationToken).Result.GetAsyncEnumerator(cancellationToken);
}

/// <summary>
/// Returns generated SQL for specific LINQ query.
/// </summary>
/// <returns>Generated SQL.</returns>
public override string ToString()
public override string? ToString()
{
return QueryProvider.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static Task<Dictionary<TKey, TSource>> ToDictionaryAsyncEF<TSource, TKey>
this IQueryable<TSource> source,
Func<TSource, TKey> keySelector,
CancellationToken cancellationToken = default)
where TKey: notnull
=> EntityFrameworkQueryableExtensions.ToDictionaryAsync(source, keySelector, cancellationToken);

/// <inheritdoc cref="EntityFrameworkQueryableExtensions.ToDictionaryAsync{TSource, TKey, TElement}(IQueryable{TSource}, Func{TSource, TKey}, Func{TSource, TElement}, CancellationToken)"/>
Expand All @@ -47,6 +48,7 @@ public static Task<Dictionary<TKey,TElement>> ToDictionaryAsyncEF<TSource,TKey,T
Func<TSource,TKey> keySelector,
Func<TSource,TElement> elementSelector,
CancellationToken cancellationToken = default)
where TKey : notnull
=> EntityFrameworkQueryableExtensions.ToDictionaryAsync(source, keySelector, elementSelector, cancellationToken);

/// <inheritdoc cref="EntityFrameworkQueryableExtensions.ToDictionaryAsync{TSource, TKey, TElement}(IQueryable{TSource}, Func{TSource, TKey}, Func{TSource, TElement}, IEqualityComparer{TKey}, CancellationToken)"/>
Expand All @@ -56,6 +58,7 @@ public static Task<Dictionary<TKey,TElement>> ToDictionaryAsyncEF<TSource,TKey,T
Func<TSource,TElement> elementSelector,
IEqualityComparer<TKey> comparer,
CancellationToken cancellationToken = default)
where TKey : notnull
=> EntityFrameworkQueryableExtensions.ToDictionaryAsync(source, keySelector, elementSelector, comparer, cancellationToken);

/// <inheritdoc cref="EntityFrameworkQueryableExtensions.FirstAsync{TSource}(IQueryable{TSource}, CancellationToken)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public class LinqToDBForEFToolsDataConnection : DataConnection, IExpressionPrepr
/// <param name="model">EF.Core data model.</param>
/// <param name="transformFunc">Expression converter.</param>
public LinqToDBForEFToolsDataConnection(
DbContext? context,
IDataProvider dataProvider,
string connectionString,
IModel? model,
DbContext? context,
[NotNull] IDataProvider dataProvider,
[NotNull] string connectionString,
IModel? model,
Func<Expression, IDataContext, DbContext?, IModel?, Expression>? transformFunc) : base(dataProvider, connectionString)
{
Context = context;
Expand All @@ -79,10 +79,10 @@ public LinqToDBForEFToolsDataConnection(
/// <param name="model">EF.Core data model.</param>
/// <param name="transformFunc">Expression converter.</param>
public LinqToDBForEFToolsDataConnection(
DbContext? context,
IDataProvider dataProvider,
IDbTransaction transaction,
IModel? model,
DbContext? context,
[NotNull] IDataProvider dataProvider,
[NotNull] IDbTransaction transaction,
IModel? model,
Func<Expression, IDataContext, DbContext?, IModel?, Expression>? transformFunc
) : base(dataProvider, transaction)
{
Expand All @@ -103,10 +103,10 @@ public LinqToDBForEFToolsDataConnection(
/// <param name="model">EF.Core data model.</param>
/// <param name="transformFunc">Expression converter.</param>
public LinqToDBForEFToolsDataConnection(
DbContext? context,
IDataProvider dataProvider,
IDbConnection connection,
IModel? model,
DbContext? context,
[NotNull] IDataProvider dataProvider,
[NotNull] IDbConnection connection,
IModel? model,
Func<Expression, IDataContext, DbContext?, IModel?, Expression>? transformFunc) : base(dataProvider, connection)
{
Context = context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public virtual void TestIdentityMapping()
using var connection = context.CreateLinqToDbConnection();

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

pk.IsIdentity.Should().BeTrue();
}
Expand All @@ -32,7 +32,7 @@ public virtual void TestNoIdentityMapping()
using var connection = context.CreateLinqToDbConnection();

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

pk.IsIdentity.Should().BeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

public virtual void WriteMessage(LogLevel logLevel, string logName, int eventId, string message, Exception exception)
{
var format = Options!.Format;
Debug.Assert(format >= ConsoleLoggerFormat.Default && format <= ConsoleLoggerFormat.Systemd);
var format = Options!.FormatterName;
Debug.Assert(format is ConsoleFormatterNames.Simple or ConsoleFormatterNames.Systemd);

var logBuilder = _logBuilder;
_logBuilder = null;
Expand All @@ -72,11 +72,11 @@ public virtual void WriteMessage(LogLevel logLevel, string logName, int eventId,
}

LogMessageEntry entry;
if (format == ConsoleLoggerFormat.Default)
if (format == ConsoleFormatterNames.Simple)
{
entry = CreateDefaultLogMessage(logBuilder, logLevel, logName, eventId, message, exception);
}
else if (format == ConsoleLoggerFormat.Systemd)
else if (format == ConsoleFormatterNames.Systemd)
{
entry = CreateSystemdLogMessage(logBuilder, logLevel, logName, eventId, message, exception);
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/LinqToDB.EntityFrameworkCore.BaseTests/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LinqToDB.EntityFrameworkCore.BaseTests.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

namespace LinqToDB.EntityFrameworkCore.BaseTests
{
Expand All @@ -12,7 +13,10 @@ public class TestUtils
.AddFilter("System", LogLevel.Warning)
.AddFilter("LinqToDB.EntityFrameworkCore.Test", LogLevel.Information)
.AddTestLogger();
.AddTestLogger(o =>
{
o.FormatterName = ConsoleFormatterNames.Simple;
});
});

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Microsoft.EntityFrameworkCore;

namespace LinqToDB.EntityFrameworkCore.SqlServer.Tests.Models.Inheritance
{
public abstract class BlogBase
{
public int Id { get; set; }
public string BlogType { get; set; } = null!;
}

public class Blog : BlogBase
{
public string Url { get; set; } = null!;
}

public class RssBlog : BlogBase
{
public string Url { get; set; } = null!;
}

public abstract class ShadowBlogBase
{
public int Id { get; set; }
public string BlogType { get; set; } = null!;
}

public class ShadowBlog : ShadowBlogBase
{
public string Url { get; set; } = null!;
}

public class ShadowRssBlog : ShadowBlogBase
{
public string Url { get; set; } = null!;
}

public class InheritanceContext : DbContext
{
public InheritanceContext(DbContextOptions options) : base(options)
{

}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BlogBase>()
.HasDiscriminator(b => b.BlogType)
.HasValue<Blog>("blog_base")
.HasValue<RssBlog>("blog_rss");

modelBuilder.Entity<BlogBase>()
.Property(e => e.BlogType)
.HasColumnName("BlogType")
.HasMaxLength(200);

modelBuilder.Entity<Blog>()
.Property(b => b.Url)
.HasColumnName("Url");

modelBuilder.Entity<RssBlog>()
.Property(b => b.Url)
.HasColumnName("Url");

modelBuilder.Entity<BlogBase>().ToTable("Blogs");

/////

modelBuilder.Entity<ShadowBlogBase>()
.HasDiscriminator()
.HasValue<ShadowBlog>("blog_base")
.HasValue<ShadowRssBlog>("blog_rss");

modelBuilder.Entity<ShadowBlogBase>()
.Property(e => e.BlogType)
.HasColumnName("BlogType")
.HasMaxLength(200);

modelBuilder.Entity<ShadowBlog>()
.Property(b => b.Url)
.HasColumnName("Url");

modelBuilder.Entity<ShadowRssBlog>()
.Property(b => b.Url)
.HasColumnName("Url");

modelBuilder.Entity<ShadowBlogBase>().ToTable("ShadowBlogs");
}

public DbSet<BlogBase> Blogs { get; set; } = null!;
public DbSet<ShadowBlogBase> ShadowBlogs { get; set; } = null!;
}

}
Loading

0 comments on commit 7411783

Please sign in to comment.