find a location for property in a new city

Thursday, 24 June 2010

Entity Framework MetadataException: Unable to load the specified metadata resource

I got a "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation" error with a description of "System.Data.MetadataException: Unable to load the specified metadata resource. at System.Data.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources".

This occurred when I was trying to move my Models out of my main MVC web application assembly. The main web dll is called Web and within that project is a folder called Models that is used to store all my *ahem* Models and entity data models etc. I decided to move everything in the Models folder out into its own Models assembly. I edited all the namespaces and everything built okay.

However, I was getting a runtime MetadataException when trying to view the page. What the hell is going on?!


The problem is that the connection string was incorrect. It wasn't immediately obviously with that error message but when you think about it, it makes sense.

My connection string was: connectionString="metadata=res://*/Models.Messaging.MessagingObjects.csdl|res://*/Models.Messaging.MessagingObjects.ssdl|res://*/Models.Messaging.MessagingObjects.msl;....etc"

Just so you know the metadata represents this metadata=res://{assembly}/{namespace}.{filename}.csdl|res://{assembly}/{namespace}.{filename}.ssdl|res://{assembly}/{namespace}.{filename}.msl;

Since I have moved the models around and the namespace is now different I needed to make it connectionString="metadata=res://*/Messaging.MessagingObjects.csdl|res://*/Messaging.MessagingObjects.ssdl|res://*/Messaging.MessagingObjects.msl;

Just for further understanding I could have replaced the asterix with Models since that is the name of the dll that contains my Messaging models.

Follow britishdev on Twitter

Monday, 21 June 2010

MVC pass ViewMasterPage a Model

I have some data that I would like to display on every page on an MVC site. So my PartialView needs to go in the ViewMasterPage with data passed to it, but how is it possible to pass and then access a ViewModel or Model in a ViewMasterPage.

I found quite a cunning way of doing this by using the ViewMasterPage<T> class instead of ViewMasterPage to model and use the data I need.


I made a new class called BaseViewModel. This will be the class that all ViewModels should now inherit from. The class looks like this:
namespace Web.ViewModels
    public class BaseViewModel
        public string MyProperty { get; set; }

I now need to make my ViewMasterPage inherit from the generic version of ViewMasterPage%lt;%gt; taking in my new BaseViewModel by making the top line look like this:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Web.ViewModels.BaseViewModel>" %>

Now I simply make the ViewModel I was using originally for my View inherit from BaseViewModel and I am ready to start passing data to the ViewMasterPage via MyProperty. The View that used the now modified ViewModel is unaffected and the ViewMasterPage now has access to the new data like so:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Web.ViewModels.BaseViewModel>" %>
<%: Model.MyProperty %>


This is all great (and very clever and amazing!) but what about when I am not passing a ViewModel to my View I am just sending a Model? I don't want to have to make a new ViewModel containing this model just for the sake of making it inherit from BaseViewModel.

Well, I made a new class that inherits from BaseViewModel that has a Generic constructor that allows me to pass in my Model. This class looks like this:
namespace Web.ViewModels
    public class BaseViewModel : BaseViewModel
        public BaseViewModel() { }
        public BaseViewModel(T innerModel)
            InnerModel = innerModel;

        public T InnerModel { get; set; }

I will need to change the type that is passed to the View in the Controller from this:
return View(message);
to this:
return View(new BaseViewModel<Message>(message));

I will then have to change the corresponding View so that it inherits from the new ViewModel and references the InnerModel property instead of just Model:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/RootMvc.Master" Inherits="System.Web.Mvc.ViewPage<Web.ViewModels.BaseViewModel<Web.Models.Social.Message>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="RootContent" runat="server">
    <%: Model.InnerModel.Title %>

Now your ViewMasterPage is still getting a ViewModel of type BaseViewModel and your main View still has strongly-typed IntelliSense access to your original Model.

Follow britishdev on Twitter

Thursday, 17 June 2010

Update minor change to database in your Entity Data Model

I added a new column to a table in the database and couldn't seem to get these changes reflected in my Entity Data Model using Entity Framework 4. There is a function I can use in the context menu called "Update Model from Database..." but that seems overkill. I just want one new column mapped in my entity data model.

I didn't want to use the 'Update Model from Database' since it will update every table and view in my model, reverting all the entity and property names to the database naming convention and breaking all my referencing code. Too much pain for what should be quite a simple change? Yes, I think so.


As an example let's say you have added a varchar(50) column called MyDbColumn to MyDbTable that currently has an entity modelled called MyEntity that hasn't recognised this column which you want to be called MyProperty.

Let's also say you have added MyProperty of type String with MaxLength set to 50 to your MyEntity entity. If not, do that. Now the tough bit: Mapping it to the database without a full update.

Time to leave the comfort of the UI.
  1. Right click your edmx file in the Solution Explorer
  2. Select 'Open With...'
  3. Select 'XML (Text) Editor' and click OK (you may be asked to close any open instances of the edmx file)
  4. Search for where is says <EntityType Name="MyDbTable">
  5. Add the property <Property Name="MyDbColumn" Type="varchar" MaxLength="50" />
  6. Search for <EntitySetMapping Name="MyEntity">
  7. You can find all your other mapped properties within a 'MappingFragment' element.
  8. Add your new mapped column <ScalarProperty Name="MyPropertyName" ColumnName="MyDbColumnName"/>
  9. Save & Build

If you now re-open your edmx file in the designer and take a look at the Mapping Details for MyEntity you will see that MyProperty is mapped to MyDbColumn in MyDbTable. Lovely stuff!

Follow britishdev on Twitter

Tuesday, 15 June 2010

Cross database joins in Entity Framework4

Quite new to Entity Framework 4 I really struggled with the concept of not being able to make an Entity Data Model containing entities of tables that live across different multiple databases. I am still really quite disappointed that EF4 cannot support this and the team behind it doesn't even seem to think that cross database entities are an issue worth considering despite how long this forum has been asking for them.

Anyway, there is a cheeky little way that it can be done. I will use the example of a forum to illustrate this. I need to display posts alongside the username and email address of the contributor of that post. The problem is that the user information lives in a separate database from the post information.

Time to unleash my MS Paint skills to illustrate:


I got around this by creating a View inside the ForumDB like so:
CREATE VIEW dbo.vUserDetails
  UserDB.dbo.UserNames u
  UserDB.dbo.ContactInfo c ON c.UserID = u.UserID

With this new View in the database I can now create an entity of it in the EDM. Go to your edmx file, right click the background and select 'Update Model from Database...'

Select the data connection to the ForumDB. Then when you are on the Choose Your Database Objects step, make sure you are on the Add tab and select Views and check the vUserDetails view:

You now have access to all your user data. You add an association to link the UserID from the Posts table (Post entity) to the UserID in your new view.


You now have SELECT access to this user data but you cannot currently INSERT/UPDATE/DELETE users. In this scenario this is okay, however if you have a different objective you can still achieve this using stored procedure in the Entities to run on INSERT functions etc.

Follow britishdev on Twitter

Monday, 14 June 2010

Entity Framework OptimisticConcurrencyException

I got an OptimisticConcurrencyException error with description of "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries." when trying to insert into a table using the Entity Framework 4.

I found that the table I was working on had an INSTEAD OF INSERT on it. This was doing a few things and then returning (0 row(s) affected). Entity Framework wasn't happy with this since it looked as though nothing was being inserted and so threw the confusing OptimisticConcurrencyException error.


I've been trying to get this to work for a fancy workaround so a drop will be the order of the day:


Job done. The main problem was finding this trigger in the first place since it is not something you normally look at and almost impossible to debug.

Better workaround

Almost 3 years later! Rachel Peirson has come up with a workaround to this problem that also allows you to keep your trigger in place. Please see her comments below for details.

Follow britishdev on Twitter

Make Form Postbacks work with MVC pages inside a WebForm MasterPage

I recently wrote about how I nested an MVC site with in a WebForms MasterPage and it how it all worked perfectly ;) However, I avoided the quite inevitable problem of POSTing not working as expected in your WebForm master page, mainly because I hadn't worked out how. Well I have now found a way and have explained it here:

Problem 1.

You may notice that any inputs above your MVC placeholder will work and any form inputs below your MVC placeholder will not POST.

The problem is that traditional ASP.NET web forms applications work off the principle of having only one form. MVC does not. However, if you look at the rendered HTML you may notice you have more than one form. ASP.NET will cope with the first form up until the first closing form tag (</form>).

Remove all form tags from your MVC pages that are causing the nested forms. This way there will only be one form per page and should therefore POST correctly. Your MVC pages will continue to POST as expected.

Problem 2.

So now your WebForms MasterPage and MVC page performs postbacks wherever there is a submit button as expected. The problem now is that you are going to have to handle that post back within your MVC page.

Make a base Controller class that all Controllers in the solution should inherit for. (I deliberately didn't end the class name with Controller so that MVC doesn't try and use it on its own).

Here is the main part of my ControllerBase:
using System.Web.Mvc;
namespace Web.Controllers
    public class ControllerBase : Controller
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
            //if there is a form being posted
            if (Request.Form != null && Request.Form.Count > 0)
                //iterate though all form keys
                for (var i = 0; i < Request.Form.Count; i++)
                    //get the value of the form key/value in question
                    var value = Request.Form[i];
                    //if this is the key/value form pair i want and it is not blank
                    if (Request.Form.Keys[i].EndsWith("txtSiteSearch") && !string.IsNullOrWhiteSpace(value))
                        //do something (this redirects to a page with the search value in the query string
                        Response.Redirect("/search/?q=" + value);

This will run on every request just before the selected ActionMethod is encountered.

I am essentially looking for a particular form field to check if the user typed into the MasterPage's site search text box (with ID="txtSiteSearch") before clicking a form submit button. If they have I send them to the search page with the search text in the querystring.

Obviously, this example is specific to my needs but if you need the form to use a service or other methods, these are all possible too.

Follow britishdev on Twitter

Tuesday, 8 June 2010

Use WebForms MasterPage in MVC project

Want to avoid having to duplicate your MasterPage design that you made in your ASP.NET WebForms project in you new MVC project? There is a simple way to share your master page across the ASP.NET web forms and MVC projects. Here's how:

Make a new "MVC 2 View Master Page" in your /Views/Shared folder and add the content:
<%@ Master Language="C#" MasterPageFile="~/MasterPages/Root.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="RootTitle" runat="server">
    <asp:ContentPlaceHolder ID="RootTitle" runat="server" />
<asp:Content ID="Content2" ContentPlaceHolderID="RootContent" runat="server">
    <asp:ContentPlaceHolder ID="RootContent" runat="server"/>

Replace the URL with the destination of your web forms MasterPage and the ContentPlaceHolderIDs with the respective ones you used in your original web forms master page.

You can then go on to use your new MVC master page as you would before but it will inherit all the content from your web forms master page.


Any form post controls on your web forms master page will not work on your MVC pages. I have found a way though which I explained in my post about making form posts work in a webform master page nested MVC page if that sort of thing interests you.

Follow britishdev on Twitter

Friday, 4 June 2010

Don't use runAllManagedModulesForAllRequests="true" when getting your MVC routing to work

It seems to be common advice to make your modules section of your web.config say <modules runAllManagedModulesForAllRequests="true">. In fact this is quite a drastic thing to do to solve the routing problem and has global effects that could CAUSE ERRORS.

You need a module that goes by the name of UrlRoutingModule-4.0 to be running through IIS. Now, since your MVC URLs are likely to end without .aspx these will not be picked up by IIS and run through the intergrated pipeline and therefore you will end up with 404 not found errors. I struggled with this when I was getting started until I found the <modules runAllManagedModulesForAllRequests="true"> workaround.

This highly recommended fix can cause other problems. These problems come in the form of making all your registered HTTP modules run on every request, not just managed requests (e.g. .aspx). This means modules will run on ever .jpg .gif .css .html .pdf etc.

This is:
  1. a waste of resources if this wasn't the intended use of your other modules
  2. a potential for errors from new unexpected behaviour.

Better solution

Fine, so the ranting about <modules runAllManagedModulesForAllRequests="true"> is over. What is a better solution?

In the modules section of your web.config, you can add the UrlRoutingModule-4.0 module in with a blank precondition meaning it will run on all requests. You will probably need to remove it first since it is most likely already registered at machine level. So make your web.config look like this:
  <remove name="UrlRoutingModule-4.0" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  <!-- any other modules you want to run in MVC e.g. FormsAuthentication, Roles etc. -->

Note: the modules element does NOT contain the runAllManagedModulesForAllRequests="true" attribute because it is evil!

Follow britishdev on Twitter