Tip of the day: Going quickly to an item in the Entity Model Diagram

After a conversation recently about how difficult it was to find stuff in the EDMX diagram because it can often be a right pigs breakfast, I stumbled across this today.

In Visual Studio there is a Model Browser that is available when viewing the diagram. It appears in the same space as the solution explorer. If you don’t see it in the tab list you can add it by going to View–>Other Windows–>Entity Data Model Browser. Like this:

Menu to open Entity Data Model Browser
Menu to open Entity Data Model Browser

Once there, you can open the tree to get the item you want much more easily that finding in on the diagram.  Open entity types to see a list:

The model browser window
The model browser window

Right-click the entity you want to move the diagram to and select “Show in Designer”

Show in Designer
Show in Designer

The designer will shift to the location of the table and put it in the centre of the window for you. It will also select the table.

It may be a really simple thing, but I wish I’d discovered it sooner.

Tip of the day: Getting Visual Studio with TFS to work offline

Earlier to day our TFS server went down. Visual Studio likes to have a constant connection open to it, but obviously that wasn’t going to happen. Luckily, it is possible to work on a solution with no connection to TFS.

If you were just starting some work…

If Visual Studio was open when TFS went off-line then it won’t allow you to check out any files. If all your files are checked in already, then you can just shut down Visual Studio and then start again. When the solution opens it detects that TFS is gone and offers to open the project in Offline mode:

Go Offline
TFS Go Offline

When TFS is available again you can simply reconnect to the server by selecting the Team?Connect to Team Foundation Server… menu. Once you are connected, you can right-click the solution and select “Go Online“.

You’ll get a dialog that asks to to confirm the files that you’ve changed in the meantime:

Go Online
TFS Go Online

It will then take a few moments for TFS to catch up (I have quite a large solution, so it took about a minute for me) then the files appeared in the Pending Changes window ready to be checked in as normal.

If you were in the middle of something

If you already had files checked out when TFS went offline then this post about converting to offline may be more useful to you.

There is also a Visual Studio extension, if you prefer not having to restart Visual Studio called Go Offline. Once installed, just to to File?Source Control?Go Offline. This may be a more useful solution if you are constantly going in and out of connection with TFS (a mobile broadband connection on a train for example).

Entity Framework: Unable to load the specified metadata resource.

Recently, I was refactoring some code and I moved the location of the .edmx file into a different folder (and new namespace). I updated the code to use the new namespace but when I ran the application a MetadataException was thrown with the message “Unable to load the specified metadata resource.”

When the location of the edmx file changes, so does the connection string. The reason is that an Entity Framework connection string does more than a normal database connection string. So, if you move the edmx file, you also have to update the connection string so that the entity framework can continue to find the resources that the edmx file defines.

The connection string contains details of how the database is mapped to the entities by referencing the CSDL (Conceptual Schema Definition Language), SSDL (Store Schema Definition Language) and MSL (Mapping Specification Language) resources which are defined in the .edmx file, so if the location of the mapping changes then the connection string also needs to be updated so that the entity framework can continue to map the database to the entities.

For example, if you have a little application with two projects, an application project (in this case, the imaginatively names ConsoleApplication2) and class library (named DataAccess). An app.config file will be created for the data access project by the entity framework tools in Visual Studio. Normally that can be copied (or just the connection string entries at least) to the app.config (or web.config) of the main application.

At this point the connection string looks like this:

provider connection string="data source=(local);
initial catalog=AdventureWorks;integrated security=True;multipleactiveresultsets=True;

As you can see it makes reference to the Products metadata in the calling assembly (that’s what the * means) which is split into the three resources (CSDL, SSDL & MSL) .

If a ModelEnties folder is created in the DataAccess project and the Products.edmx is moved into the ModelEntities folder then the location of the resource is moved, so the connection string is no longer valid. So, for the change that was just made the connection string needs to be updated to look like this:

provider=System.Data.SqlClient;provider connection string="data source=(local);initial catalog=
AdventureWorks;integrated security=True;

I’ve bolded the bits that have changed.

If you want to quickly get an updated connection string, you can open the edmx file and click in the design area then press F4 (or the menu View→Properties Window). The window will show Connection String property which can be copied and pasted into the config file.

Tip of the Day #23: Getting going with IIS Express

First, if you don’t have it already you need to download IIS Express (you can also use this link to get the full install, not via Microsoft’s web installer, if you are behind a proxy that is preventing the installation). And, I’d also recommend downloading Visual Studio 2010 SP1 and upgrading to it.

In your web project, open up the properties by right clicking the project and selecting properties, or pressing Alt+Enter while the project is selected.

You will then be presented with a view like this:

1 Initial Web Properties

By default, in the servers section of the Web tab the “Use Visual Studio Development Server” (aka Cassini) will be selected. Change this to “Use Local IIS Web Server”

2 Change to Local IIS Web Server

If you want to customise the settings you may do so. I tend to set a specific local port so that I know that all my applications don’t class with one another and that I can easily identify it later. My naming scheme to select 4 or 5 digits that are derived from the name of the project as if dialled into a telephone keypad. (Some people think that’s a bit weird but it makes it easy to avoid port clashes and to reverse the port into the project if you ever get lost.)

If necessary you can define the virtual directory in the Project URL and configure it by pressing “Create Virtual Directory”.

3 Setting a Virtual Directory

If you don’t “Create Virtual Directory” and you attempt to run the project, you’ll get a warning dialog that asks if you want to configure it. If you select yes, then Visual Studio will configure the virtual directory for you and start the application.

4 Not configuring a Virtual Directory

Finally, if you need to see what Sites IIS Express is running there is a tray icon you can right click on to see.

5 System Tray Icon for IIS Express

And if you click “Show all applications” you get to see all the sites that IIS Express is running. Clicking on a URL takes you to that site, anywhere else on the line will bring up details of the site in the lower part of the dialog.

6 IIS Express Running Applications

Clicking on the “Parent” name will take you to the instance of Visual Studio that the application is running from. This is a really nifty feature to get you back to the correct instance of Visual Studio if you are running many of them at once.

Clicking on the “Path” will open up Windows Explorer to show you the folder in which the site is located. And clicking “Config” will open the config file in Visual Studio.