How do I Use FastMember to create a reader of an object with child object

c# fastmember idatareader sqlbulkcopy

Question

We shouldSQLBulkCopy a collection we possess And I attempted to utilize FastMember for that as we require an IDataReader to use SQLBulkCopy. As far as I'm aware, we can utilizeFastMember to develop anIDataReader an IEnumerable from.

My code is therefore:

using (var reader = ObjectReader.Create(customer.Records)
{
    DataAccess.BulkInsert(serverName, databaseName, tableName, reader);
}

The problem, however, is that every record has a child object named that contains additional characteristics that I want to have in data readers so that they may be placed into the table in the database. However, I can't seem to obtain a flatness.IDataReader

1
1
4/27/2018 4:21:13 PM

Accepted Answer

I created an app to address this problem.anonymous object Then putting the data we need inside of it. By doing so, you may flatten down your object and utilize just the necessary characteristics.

var newObjects = customer.Records.Select(record => new
{
    customer.Id,
    record.DisplayName,
    record.TotalCount,
    record.Conditions.IsActive,
    record.Conditions.SusperCondition.IsSuper,
    Status = (int)record.Status
});

then just provide this new thing to them.ObjectReader

using (var reader = ObjectReader.Create(newObjects))
{
    DataAccess.BulkInsert(serverName, databaseName, tableName, reader, SetBulkCopyMappings);
}

Large Insert -

internal static void BulkInsert(string serverName, string databaseName, string tableName, IDataReader dataReader, Action<SqlBulkCopy> setMappings)
    {
        using (var bulkCopy = new SqlBulkCopy(BuildConnectionString(serverName, databaseName)))
        {
            bulkCopy.DestinationTableName = tableName;
            setMappings(bulkCopy);
            bulkCopy.WriteToServer(dataReader);
        }
    }
2
4/27/2018 10:48:42 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow