Still Stumped: Entity Framework 6 and ‘Bounded Contexts’

Ok, I still can’t figure out how to do this.  It has to be possible.  My Google-Fu is usually phenomenal, but it is failing me here. 

I’ve ‘dumbed it down’ to a very simple scenario.  Suppose I have two EF contexts.  Each one references two tables, the same two tables, from the same database, and I use the ‘Generate from Database’ option in EF 6.  In this case, I am mimicking  two bounded contexts, the shopping BC and the checkout BC.  Each one needs to reference the Cart and CartItem tables and generate those entities accordingly.  Here are some pictures to illustrate.

Shopping BC:


Here’s the Checkout BC:


What I expect, and what was possible using EF 5 and before, using the Legacy ObjectContext option, was that by adding custom namespaces, I could have, e.g., a Shopping.Cart object and a Checkout.Cart object, which will be created using different contexts, and used in different scenarios.  It should be obvious why this is important.

However, what actually happens is this:


Depending on I know not what (what file is in focus?  Barometric pressure?  The Yankees’ pitching staff’s ERA?), the Cart and CartItem classes will be generated by T4 either under or  Always together, but always under one.  They should be generated under both, with different namespaces.

As readers of this blog know, I am lazy, stupid, slow and old.  Something just out of the box should work here.  But, even though I am lazy, stupid, slow and old, I’m willing to hack some .tt files if needed to get this to work, as I don’t want to be ‘stuck’ using Entity Framework 5 for the rest of my fricking life. 

I was hoping, as has happened in the past, that by writing this blog post out, I would stumble upon the obvious solution.  I’m still hoping for that, as it often happens after the ‘publish’ button is pressed.

Why doesn’t this just work?

posted on Friday, July 25, 2014 7:57 PM Print
# re: Still Stumped: Entity Framework 6 and ‘Bounded Contexts’
7/26/2014 11:48 PM
My guess is that it is because they are in the same name space, and you can't have two of the same class in the same ns. Try putting the two contexts in different folders of the project
# re: Still Stumped: Entity Framework 6 and ‘Bounded Contexts’
7/27/2014 12:03 AM

I do put them in different namespaces though, that's what the comment about custom namespaces is about. I do that with EF 5 and it works just fine.

So, there should be a Checkout.Cart and a Shopper.Cart, but instead, it will fall into one or the other, but not both.
# re: Still Stumped: Entity Framework 6 and ‘Bounded Contexts’
7/30/2014 12:31 PM

Actually, for the heck of it, I tried your suggestion, just to see, and I can now build the project, as it creates the relevant questions.

However, at run time, it complains about ambiguous mapping. Now looking into that issue.

Thanks for the suggestion. I don't quite understand why it 'works' but it did make a difference.

Post Comment

Title *
Name *
Comment *  
Please add 6 and 2 and type the answer here: