Skip to content

Commit

Permalink
Merge pull request #176 from linq2db/version6
Browse files Browse the repository at this point in the history
Release 6.2.0
  • Loading branch information
sdanyliv authored Sep 15, 2021
2 parents 30945f9 + ea55615 commit e63148c
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 11 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>6.1.0</Version>
<Version>6.2.0</Version>

<Authors>Svyatoslav Danyliv, Igor Tkachev, Dmitry Lukashenko, Ilya Chudin</Authors>
<Product>Linq to DB</Product>
Expand Down
4 changes: 2 additions & 2 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" />

Expand Down
25 changes: 23 additions & 2 deletions Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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 @@ -174,7 +192,7 @@ static DataType DbTypeToDataType(DbType dbType)
_ => DataType.Undefined
};
}

public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = true) where T : Attribute
{
if (typeof(Expression).IsSameOrParentOf(type))
Expand All @@ -187,9 +205,11 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
{
var props = et.GetProperties();
var prop = props.FirstOrDefault(p => CompareProperty(p, memberInfo));

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

var isPrimaryKey = prop.IsPrimaryKey();
var primaryKeyOrder = 0;
if (isPrimaryKey)
Expand Down Expand Up @@ -255,6 +275,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 @@ -160,7 +160,8 @@ 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>
Expand Down
2 changes: 1 addition & 1 deletion Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public static MappingSchema GetMappingSchema(
/// <param name="ctx">Optional DbContext instance.</param>
/// <param name="model">EF Core data model instance.</param>
/// <returns>Transformed expression.</returns>
public static Expression TransformExpression(Expression expression, IDataContext dc, DbContext? ctx, IModel? model)
public static Expression TransformExpression(Expression expression, IDataContext? dc, DbContext? ctx, IModel? model)
{
return Implementation.TransformExpression(expression, dc, ctx, model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ private void OnEntityCreatedHandler(EntityCreatedEventArgs args)
var assignExpr = Expression.Assign(variable, Expression.Convert(objParam, entityType.ClrType));

var key = entityType.GetKeys().FirstOrDefault();
if (key == null)
return null;

var arrayExpr = key.Properties.Where(p => p.PropertyInfo != null || p.FieldInfo != null).Select(p =>
Expression.Convert(Expression.MakeMemberAccess(variable, p.PropertyInfo ?? (MemberInfo)p.FieldInfo),
Expression.Convert(Expression.MakeMemberAccess(variable, p.PropertyInfo ?? (MemberInfo)p.FieldInfo!),
typeof(object)))
.ToArray();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Linq;
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 interface IVersionable
{
int Id { get; set; }
int? ParentVersionId { get; set; }
}

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

private void VersionEntity()
{
ChangeTracker.DetectChanges();
var modifiedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified && e.Entity is IVersionable);

foreach (var modifiedEntry in modifiedEntries)
{
var cloned = (IVersionable)Activator.CreateInstance(modifiedEntry.Entity.GetType());
modifiedEntry.CurrentValues.SetValues(cloned);

// rollback
modifiedEntry.CurrentValues.SetValues(modifiedEntry.OriginalValues);

cloned.Id = 0;
cloned.ParentVersionId = ((IVersionable)(modifiedEntry).Entity).Id;

Add((object)cloned);
}
}

public override int SaveChanges()
{
return base.SaveChanges();
}

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<Blog>().ToTable("Blogs");
modelBuilder.Entity<RssBlog>().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<ShadowBlog>().ToTable("ShadowBlogs");
modelBuilder.Entity<ShadowRssBlog>().ToTable("ShadowBlogs");
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void ConfigureEntityFilter<TEntity>(ModelBuilder builder)
{
NorthwindContext? obj = null;

builder.Entity<TEntity>().HasQueryFilter(e => !obj!.IsSoftDeleteFilterEnabled || !e.IsDeleted);
builder.Entity<TEntity>().HasQueryFilter(e => !obj!.IsSoftDeleteFilterEnabled || !e.IsDeleted || !EF.Property<bool>(e, "IsDeleted"));
}

public bool IsFilterProducts { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
variables:
solution: 'linq2db.EFCore.sln'
build_configuration: 'Release'
assemblyVersion: 6.1.0
nugetVersion: 6.1.0
assemblyVersion: 6.2.0
nugetVersion: 6.2.0
artifact_nugets: 'nugets'

# build on commits to important branches (master + release branches):
Expand Down

0 comments on commit e63148c

Please sign in to comment.