Skip to content

Commit

Permalink
Added missed information about table for EntityService. (#3170)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdanyliv committed Aug 11, 2021
1 parent 8314129 commit c321b9f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
25 changes: 25 additions & 0 deletions Source/LinqToDB/EntityCreatedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,30 @@ public class EntityCreatedEventArgs
/// DataContext that created a new entity.
/// </summary>
public IDataContext DataContext { get; set; } = null!;

/// <summary>
/// TableOptions of the current entity
/// </summary>
public TableOptions TableOptions { get; set; }

/// <summary>
/// TableName of the current entity
/// </summary>
public string? TableName { get; set; }

/// <summary>
/// SchemaName of the current entity
/// </summary>
public string? SchemaName { get; set; }

/// <summary>
/// DatabaseName of the current entity
/// </summary>
public string? DatabaseName { get; set; }

/// <summary>
/// SchemaName of the current entity
/// </summary>
public string? ServerName { get; set; }
}
}
24 changes: 19 additions & 5 deletions Source/LinqToDB/Linq/Builder/TableBuilder.TableContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,21 @@ Expression BuildTableExpression(bool buildBlock, Type objectType, Tuple<int, Sql
}

[UsedImplicitly]
static object OnEntityCreated(IDataContext context, object entity)
static object OnEntityCreated(IDataContext context, object entity, TableOptions tableOptions, string? tableName, string? schemaName, string? databaseName, string? serverName)
{
var onEntityCreated = context.OnEntityCreated;

if (onEntityCreated != null)
{
var args = new EntityCreatedEventArgs
{
Entity = entity,
DataContext = context
Entity = entity,
DataContext = context,
TableOptions = tableOptions,
TableName = tableName,
SchemaName = schemaName,
DatabaseName = databaseName,
ServerName = serverName
};

onEntityCreated(args);
Expand All @@ -332,16 +337,25 @@ static object OnEntityCreated(IDataContext context, object entity)
return entity;
}

private static readonly MethodInfo _onEntityCreatedMethodInfo = MemberHelper.MethodOf(() =>
OnEntityCreated(null!, null!, TableOptions.NotSet, null, null, null, null));

Expression NotifyEntityCreated(Expression expr)
{
if (Builder.DataContext is IEntityServices)
{
expr =
Expression.Convert(
Expression.Call(
MemberHelper.MethodOf(() => OnEntityCreated(null!, null!)),
_onEntityCreatedMethodInfo,
ExpressionBuilder.DataContextParam,
expr),
expr,
Expression.Constant(SqlTable.TableOptions),
Expression.Constant(SqlTable.PhysicalName, typeof(string)),
Expression.Constant(SqlTable.Schema, typeof(string)),
Expression.Constant(SqlTable.Database, typeof(string)),
Expression.Constant(SqlTable.Server, typeof(string))
),
expr.Type);
}

Expand Down
5 changes: 4 additions & 1 deletion Tests/Linq/Linq/EntityCreatedTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq;

using FluentAssertions;
using LinqToDB;

using NUnit.Framework;
Expand Down Expand Up @@ -34,6 +34,9 @@ void EntityCreated(EntityCreatedEventArgs args)
{
if (_checkEntityIdentity && args.Entity is Parent p)
{
args.TableOptions.Should().Be(TableOptions.NotSet);
args.TableName.Should().Be(nameof(Parent));

if (_parents.TryGetValue(p.ParentID, out var pr))
{
args.Entity = pr;
Expand Down

0 comments on commit c321b9f

Please sign in to comment.