Entity Framework Power Tool Tips to View Model (A Feature I Depend On!)

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 m ay have a set of classes but a DbContext that only specifies a single DbSet:

public class CustomerServiceContext : DbContext
{
public DbSet<Customer> Customers { get; set; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }


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


image


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.


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).


SNAGHTMLb0b6d0b


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.


image


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.


SNAGHTMLb07a092

 •  0 comments  •  flag
Share on Twitter
Published on May 27, 2013 09:45
No comments have been added yet.


Julia Lerman's Blog

Julia Lerman
Julia Lerman isn't a Goodreads Author (yet), but they do have a blog, so here are some recent posts imported from their feed.
Follow Julia Lerman's blog with rss.