3

Looking at questions like this I am inclined not to use several dbcontexts. However, it would still be nice to have a look at some example code. Is anyone aware of an enterprise level open source implementation (c#) that uses several dbcontexts and unit of work (and potentially asp.net mvc)?

Community
  • 1
  • 1
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • In addition to the information in the linked question, in EF6 you can have migrations with multiple DbContexts and have them in one project. You'll need to specify what context you are talking about when running migrations. – trailmax Jul 23 '14 at 14:51

1 Answers1

5

As you have used the word Enterprise I would point you at Julie Lerman, who has some nice coverage of multiple DbContexts from the perspective of DDD bounded contexts.

Heres a relatively recent MSDN article she's written on this subject: http://msdn.microsoft.com/en-us/magazine/jj883952.aspx.

She also has a fantastic series of screencasts on Pluralsight that cover enterprise use of Entity Framework, including the use of multiple contexts. I wont include any links as this is a paid-for service.

From a code sample perspective, take a look at the reference CQRS architecture on the Microsoft Patterns & Practices which uses bounded contexts (as multiple DbContexts) to decompose a complex system into separate domains. http://msdn.microsoft.com/en-us/library/jj554200.aspx

The code for the reference CQRS architecture is here: https://github.com/mspnp/cqrs-journey-code.

Hope some of this proves useful to you.

Matt Caton
  • 3,473
  • 23
  • 25
  • 1
    If you EVER have ANY question about Entity Framework, the answer should always be Julie Lerman. – krillgar Jul 23 '14 at 13:28
  • I read (parts) of her books thanks. I am aware of her blog on dbcontext and bounded context. I have mentioned this here: http://stackoverflow.com/questions/23949971/ef-context-database-schema-ddd-boundedcontext However, there are also some differences, according to some ddd fundamentalists (see comments in the mentioned SO post). Anyway, I cannot see how Julie implements the unit of work pattern (also accross dbcontexts/bounded contexts). – cs0815 Jul 23 '14 at 14:03
  • Thanks for the link to your question - you've asked some very interesting questions around this area. Looking forward to a cup of tea later and reading through them – Matt Caton Jul 23 '14 at 14:30
  • Unit of work in EF is already implemented internally, no need to create a surplus wrapper for DbContext. And if you are talking about UoW across the bounded contexts, this is a hint for you, that these entities should probably stay within one context. – trailmax Jul 23 '14 at 14:49
  • @LongboatHarry - thanks for being so positive - very refreshing in comparison to most SO users these days (-: – cs0815 Jul 23 '14 at 14:53
  • @trailmax - good point. that's how the ddd community determines boundaries (things changed in one transaction). I understand that some people say that dbcontext = uow and repository = dbset. I still think that setting dbcontext = ddd bounded context is wrong though. – cs0815 Jul 23 '14 at 16:28