1

I am trying to Mock EF on IDBcommandInterceptor For insert / update operations it is enough simple - I can return a DbDataReader made of a single field or an int However, for select operations, if there are some "include", then the shape of the sql result is pretty ... awesome

How could I obtain from

ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> the fields and the names and the corresponding entities for the DbDataReader result?

Thank you,

Example : Trying to read Department(Id, Name) from Id with include on Employee(Id, Name .IDDepartment, DateModification, DateCreation, User) The Command that obtains the DBDataReader for an include is below .

I want to know the fields names (like C1, ID1, Name1 and others) to be capable of Mocking.

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
Bogdan Sahlean
  • 19,233
  • 3
  • 42
  • 57

1 Answers1

0

You should be able to get the column names from the reader by doing something like this

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

That in the case you only need the names. If you get the whole schema for the result table, you could get it by doing this:

DataTable schemaTable = reader.GetSchemaTable();

And then iterate through the collection of rows in the datatable to get the properties of each column.

Does that help to answer your question?

Sergio A.
  • 403
  • 2
  • 9
  • What you say here it is how to transform a IDataReader into an datatable . Please read again the question - I do not have a IDataReader - nor I want to run the DbCommand to the database. – ignatandrei Oct 08 '14 at 02:50