如何为Entity Framework创建的SQL语句获得更好的表别名?

时间:2014-07-30 05:42:06

标签: .net entity-framework entity-framework-6.1

是否可以控制实体框架为其创建的SQL语句使用哪些表别名?例如,而不是

SELECT [Extent1].[Id] AS [Id]
FROM [dbo].[Orders] AS [Extend1]
INNER JOIN [dbo].[Customers] AS [Extend2] ON [Extend1].[CustomerId] = [Extend2].[Id]
WHERE [Extent2].[Name] = 'xyz'

我想

SELECT [order].[Id] AS [Id]
FROM [dbo].[Orders] AS [order]
INNER JOIN [dbo].[Customers] AS [customer] ON [order].[CustomerId] = [customer].[Id]
WHERE [customer].[Name] = 'xyz'

或类似的东西,只要我不记得哪个扩展号是哪个表。当然,在这个简单的示例中,它没有问题,但在您[Extend1][Extend7]的查询中,它真的变得复杂了。

1 个答案:

答案 0 :(得分:2)

您可以使用IDbCommandInterceptor并覆盖CommandText

我刚创建了一个非常简单的逻辑,用正则表达式替换别名。

以下代码

var query = (from f1 in db.Foos
            where f1.ParentFooId == null
            join f2 in db.Foos on f1.FooId equals f2.ParentFooId
            select f2).ToArray();

默认情况下会变成

SELECT 
    [Extent1].[FooId] AS [FooId], 
    [Extent2].[FooId] AS [FooId1], 
    [Extent2].[FooName] AS [FooName], 
    [Extent2].[ParentFooId] AS [ParentFooId], 
    [Extent2].[ParentFoo_FooId] AS [ParentFoo_FooId]
    FROM  [dbo].[Foos] AS [Extent1]
    INNER JOIN [dbo].[Foos] AS [Extent2] ON [Extent1].[FooId] = [Extent2].[ParentFooId]
    WHERE [Extent1].[ParentFooId] IS NULL
go

然后我在ReaderExcecuting上拦截了命令文本。

public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
    var aliasLookup = new Dictionary<string, string>();
    var tableLookup = new Dictionary<string, int>();

    var text = command.CommandText;
    // Looking something like "[dbo].[Foos] AS [Extent1]"
    var pattern = @"\[dbo\]\.\[([\w\d]*)\] AS \[(Extent\d*)\]";
    var matches = Regex.Matches(text, pattern);
    foreach(Match m in matches)
    {
        if (m.Groups.Count == 3)
        {
            var tableName = m.Groups[1].Value;
            var aliasName = m.Groups[2].Value;

            var replacement = tableName;
            if (tableLookup.ContainsKey(tableName)) replacement += tableLookup[tableName]++;
            else tableLookup.Add(tableName, 2);

            aliasLookup.Add(aliasName, replacement);
        }
    });

    foreach(var pair in aliasLookup)
    {
        var oldAliasName = string.Format("[{0}]", pair.Key);
        var newAliasName = string.Format("[{0}]", pair.Value);
        command.CommandText = command.CommandText.Replace(oldAliasName, newAliasName);
    }
}

结果看起来像

SELECT 
    [Foos].[FooId] AS [FooId], 
    [Foos2].[FooId] AS [FooId1], 
    [Foos2].[FooName] AS [FooName], 
    [Foos2].[ParentFooId] AS [ParentFooId], 
    [Foos2].[ParentFoo_FooId] AS [ParentFoo_FooId]
    FROM  [dbo].[Foos] AS [Foos]
    INNER JOIN [dbo].[Foos] AS [Foos2] ON [Foos].[FooId] = [Foos2].[ParentFooId]
    WHERE [Foos].[ParentFooId] IS NULL
go

这只是让你知道如何做到这一点,逻辑还没有经过复杂查询的测试,它应该只用于调试。