ID在IDBcommandInterceptor和DBDataReader上

时间:2014-09-25 06:45:43

标签: entity-framework idbcommand

我想在IDBcommandInterceptor上模拟EF 对于插入/更新操作,它足够简单 - 我可以返回由单个字段或int组成的DbDataReader 但是,对于选择操作,如果有一些" include",那么sql结果的形状很漂亮... awesome

我怎样才能从

获得

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

谢谢,

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

我想知道字段名称(例如C1ID1Name1和其他字段)能够进行模拟。

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

1 个答案:

答案 0 :(得分:0)

你应该能够通过这样的方式从读者那里获得列名

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

在这种情况下你只需要名字。如果你得到结果表的整个模式,你可以通过这样做得到它:

DataTable schemaTable = reader.GetSchemaTable();

然后遍历数据表中的行集合以获取每列的属性。

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