When defining models with Entity Framework’s Code First and a DbContext class, I find the “View Model” feature of the Entity Framework Power Tool completely indispensable. Without it, I only *think* I know what I’ve told EF my model should be but I don’t really know how Code First will interpret the information I’ve provided (class structures, DbSets, configurations).
For example, I may have a set of classes but a DbContext that only specifies a single DbSet:
public class CustomerServiceContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
But because of relationships from Customer to other types and their relationships to even other types, using it’s base rules (convention) Code First interprets your entire model as
Another common point of mapping confusion is with relationships. Being able to visualize how Code First interprets the model can show me where, for example, I’ve attempted to define a 1:1 relationship but in fact, I’ve defined a 1:0..1 which will really create problems down the road in my application.
I use the tool to show me where I haven’t configured things properly and fix my mappings (via convention or configurations) until the visual model aligns with my intention.
So I really can’t live without this tool and I show it to everyone I can.
Currently the tool is something you install as an extension to Visual Studio. It’s called Entity Framework Power Tools Beta 3 and it works even wtih the latest update to VS (Visual Studio 2012 Update 2).
While there are other features, with the tool installed, you can point to any class that inherits from DbContext, right click and see “Entity Framework” on the context menu. Select Entity Framework and you’ll see four options. My focus is on the first of these “View Entity Data Model (Read-only).
But half the time I show it, it doesn’t work and it’s always something I’ve done wrong and forgotten about.
Designer Problems != Modeling Problems
If there is a problem with your model, you’ll get a helpful error message in the output window. This is about what’s wrong with your mappings. One common one is you tried to create a one to one mapping but haven’t given code first enough detail (e.g. it needs to know which is the principal and which is the dependent).
The focus of this blog post however is problems I encounter with getting the tool to perform it’s task.
Most Common Reason that the Designer Fails: Can’t Find the Connection String
Even though the designer is not going to touch the database to generate the view, it still needs to have access to a connection string.
But when it can’t find it, it doesn’t always tell you that it can’t find the connection string.
Typically I get an error message that says: “sequence contains no matching element”.
Another is “Exception has been thrown by the target of an invocation”.
Designer Looks for the Connection String in the Startup Project of a Solution
This is the A#1 reason that the designer won’t work. It’s a bit of a pain because you don’t really want to set things up for the designer, but for debug mode. It doesn’t matter if the startup project is executable. It just has to be some project with a config file and in the config file, valid connection information ( a connection string or connection info in the Entity Framework section) that EF will look for.
Designer Will Not Look Inside Solution or Project Folders
This is my A#2 reason the designer won’t work. I like to organize my solutions. I organize projects into solution folders and I organize code into project folders. If you config file is in a folder, the designer won’t find it.
My Typical Setup for Success
So I have a separate project in my solution that is NOT inside a folder just to help me use this critical designer tool. Thanks to Vaughn Vernon for helping me find a better name for the project than I had been using that makes it very clear that this has nothing to do with my application at all.
So when I need to view a model, as long as I remember to set that project as the startup project in my solution, the designer is able to do it’s job. Here is an example of using this for a context that doesn’t even inherit DbContext directly. It inherits from my own class “BaseContext” which inherits from DbContext. The designer is able show me the visual model even with this extra abstraction.
Another Weird Error that Is Not About the Connection String: “A constructible type …”
Thanks to Ethan in comments for pointing out this one. I actually had it the day *before* I wrote this post and had forgotten (short term memory issues…old age…excuses excuses 🙂 ).
‘A constructible type deriving from DbContext could not be found in the selected file’.
This was misleading because like Ethan, the fact that I was deriving from BaseContext led me to believe that *this* was the cause for not being able to find the DbContext type. But changing CustomerServiceContext to inherit DbContext directly did not fix my problem.
I found this StackOverflow thread about this error message with a response from Brice Lambson of the EF team that suggested an Visual Studio extension was the issue. I had just added two extensions for spell checking my code and comments. One was HTML Spell Checker (from Microsoft), the other was Multi Language Spell Checker (also from someone at Microsoft). So I thought I would disable one at a time to see if one was the culprit. I happened to pick HTML Spell Checker to disable first and voila, the View Entity Data Model tool worked again (after a VS restart). Fortunately the second extension is the one I had really wanted anyway. I believe that this problem will be resolved by the time we get the tools integration in EF6.