0

I have this code:

using (var context = new MyContext())
{
    var uow = new UnitOfWork(context);
    var userService = new UserService(uow);
    var gameService = new GameService(uow);

    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    userService.AddUser();
    gameService.AddGame();
    uow.Commit();

    Console.Read();
}

Which should produce a single hit to the database. I'm looking at SQL Profiler and it looks like multiple hits (or am I wrong?). Here's a screenshot (Don't mind the name of the tables I was just inserting fake data in tables that had no consequences)

enter image description here

Is this just showing the multiple inserts or is this separate hits to the database?

EDIT: Larger image is here

Ryan
  • 4,354
  • 2
  • 42
  • 78
  • Separate hits. See one line: it is an insert and a select for the allotted Id, that is one hit. As far as I know NHibernate is the only orm that wraps multiple queries in one package (apart from this common insert-select pair). – Gert Arnold Feb 25 '12 at 21:41
  • @GertArnold - There's no way in EF to add multiple entties and then commit that? I thought that was the whole point of UnitOfWork pattern – Ryan Feb 25 '12 at 21:50
  • When you consider one hit as one statement being sent to the database EF would have to build one statement with row constructors (when acting on sql server 2008) but it would have a hard time getting the generated id's back to the new objects. – Gert Arnold Feb 25 '12 at 21:58
  • Is there a workaround for batch-inserts? – Ryan Feb 25 '12 at 22:02
  • Not really, unless you mix technologies: http://stackoverflow.com/questions/1609153/how-to-do-a-bulk-insert-linq-to-entities – Gert Arnold Feb 25 '12 at 22:12
  • @GertArnold - that's unbelievable.. this is such a common practice, I wonder if they plan on implementing this feature soon – Ryan Feb 25 '12 at 22:24
  • It's a unit of work because all the inserts happen in the same transaction, so all the work either completes or all the work fails. One unit of work could span multiple commands, even multiple connections. – OdeToCode Feb 25 '12 at 23:27

2 Answers2

1

I see one connection being opened looking at the Audit Login and then 11 insert statements, then one Audit Logout. This seems like the way it should work, nothing wrong with it. Why do u think his are multiple hits? What is a hit in your humble opinion :)

rfcdejong
  • 2,219
  • 1
  • 25
  • 51
  • Good enough for me :) I wasn't sure if the Audits where considered the 'hits' or not – Ryan Feb 25 '12 at 21:36
  • I was mistaken I guess you can't do this with EF :( – Ryan Feb 25 '12 at 22:23
  • Are u going to do a high load of inserts then? For a 'normal' business application it is normal just to insert this way. If u have some sort of other requirement for massive inserts then use SSIS or another database technology NoSQL or so ;) – rfcdejong Feb 25 '12 at 22:32
  • 1
    Your real question should have been something like 'Bulk insert with Entity Framework' which is indeed not possible, maybe i have mistaken your question hehe. As GertArnold said, you will have to mix technologies and use something closer to ADO.NET http://cgeers.com/2011/05/19/entity-framework-bulk-copy/ – rfcdejong Feb 25 '12 at 22:38
1

I thought I'd summarize the comments.

Unfortunatly EF 4.3 doesn't support Bulk operations. Recommended approach is to either use SqlBulkCopy explained here or if your application is performance critical use a NoSQL approach.

Ryan
  • 4,354
  • 2
  • 42
  • 78