I have 2 different units of work: one based on ADO.NET, calling stored procedures mostly (uowADO
) and another one using Entity Framework 6 (
Unfortunately I am not sure how to design such Unit of Work to make it generic so that I could use it in all cases from BL and for both data access layers: ADO.NET and EF.
start by realizing that EF has hno way to access the database. It relies on ADO.NET for that. AT the end, it uses commands, datareaders etc. to do the low level work.
It is trivial to set things up in such a way that you can extract the connection under a EF context and then use ADO.NET yourself directly.
As EF has zero support in any way for doing efficient even low volume bulk operations - you can / have to rely on ADO.NET anyway. EF itself is a perfect anti-example from a SQL point of view on how to handle the database for larger volumes. It isuses one update / insert per row - although (even ignoring SqlBulkCopy for example) it could for most databases issue one for multiple rows as the syntax perfectly allows that.
As aked for it: Getting to the underlying connection is really trivial when you like reading documentation of samples. This is the code I use for bulk inserts. The ObjectBulkCopy class is a little more complex, but here one can see how get the database connection:
public static void BulkInsert(this Repository repository, IEnumerable objects) where T : class
{
var bulkCopy = new ObjectBulkCopy();
var connection = (SqlConnection) repository.Database.Connection;
bulkCopy.Insert (objects, connection);
}
Seriously trivial - it is there in any repository as a property, ready for casting.