1

我正在尝试在IDBcommandInterceptor 插入/更新操作上模拟 EF,这很简单 - 我可以返回DbDataReader由单个字段或 int 组成的但是,对于选择操作,如果有一些“包含”,那么 sql 结果的形状很漂亮……棒极了

我怎么能从

ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader>结果的字段和名称以及相应的实体DbDataReader

谢谢,

示例:尝试读取Department(Id, Name) from Id with include on Employee(Id, Name .IDDepartment, DateModification, DateCreation, User) 获取包含的 DBDataReader 的命令如下。

我想知道能够模拟的字段名称(如C1ID1和其他)。Name1

SELECT 
    [Project2].[Id] AS [Id], 
    [Project2].[Name] AS [Name], 
    [Project2].[C1] AS [C1], 
    [Project2].[Id1] AS [Id1], 
    [Project2].[Name1] AS [Name1], 
    [Project2].[IDDepartment] AS [IDDepartment], 
    [Project2].[DateModification] AS [DateModification], 
    [Project2].[DateCreation] AS [DateCreation], 
    [Project2].[User] AS [User], 
    [Project2].[Archive] AS [Archive]
    FROM ( SELECT 
        [Limit1].[Id] AS [Id], 
        [Limit1].[Name] AS [Name], 
        [Extent2].[Id] AS [Id1], 
        [Extent2].[Name] AS [Name1], 
        [Extent2].[IDDepartment] AS [IDDepartment], 
        [Extent2].[DateModification] AS [DateModification], 
        [Extent2].[DateCreation] AS [DateCreation], 
        [Extent2].[User] AS [User], 
        [Extent2].[Archive] AS [Archive], 
        CASE WHEN ([Extent2].[Id] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
        FROM   (SELECT TOP (1) 
            [Extent1].[Id] AS [Id], 
            [Extent1].[Name] AS [Name]
            FROM [dbo].[Department] AS [Extent1]
            WHERE [Extent1].[Id] = @p__linq__0 ) AS [Limit1]
        LEFT OUTER JOIN [dbo].[Employee] AS [Extent2] ON [Limit1].[Id] = [Extent2].[IDDepartment]
    )  AS [Project2]
    ORDER BY [Project2].[Id] ASC, [Project2].[C1] ASC
4

1 回答 1

0

您应该能够通过执行以下操作从阅读器获取列名

var columns = Enumerable.Range(0, reader.FieldCount) .Select(reader.GetName).ToList();

在这种情况下,您只需要名称。如果您获得结果表的整个架构,您可以通过以下方式获得它:

DataTable schemaTable = reader.GetSchemaTable();

然后遍历数据表中的行集合,得到每一列的属性。

这有助于回答你的问题吗?

于 2014-10-06T20:49:37.633 回答