Entity Framework v4.2 upgrade not working

This particular error caused me some headaches so I decided to blog about it in the hope that it'll help some other poor soul! I have a pretty standard MVC 3 project that uses Entity Framework Code First for handling the database side of things. I decided to update my project to ensure it was using the latest version of all of the packages in use on my project (ninject/entity framework/etc). This particular project was using Entity Framework v4.1, which I had previously updated using NuGet without any problems, so I decided to download and install the latest version from NuGet - ver 4.2.

The Problem:

I know after installing any new version of Entity Framework it's always wise to restart Visual Studio and do a clean rebuild of your project but when I went to run this project locally it threw up the following error:

 

Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

 

It was fairly obvious that my project was looking for the wrong version of EF but I couldn't tell why. I went looking for any hard coded mention of EFv4.1 but nothing was returned. Next I did some searching online but nothing seemed to match my particular error. I took a closer look at the detailed error log and noticed it was looking for the entityframework.dll file but was not finding it.

The Solution:

I took a look in the 'Packages' folder that NuGet installs the files to. I could see 2 Entity Framework folders. One folder was for the older v4.1 and one was for the new v4.2. I took a look inside the v4.2 folder  and could see the dll files in there so I knew the files had downloaded. Then I took a look in the v4.1 folder and noticed that the folder structure was different.

  • I went back into the v4.2 folder (Packages\EntityFramework.4.2.0.0\lib\) and could see a folder called 'net40' that contained the dll files.
  • I cut the 2 files out of there and placed them into the 'Packages\EntityFramework.4.2.0.0\lib' folder (up 1 folder level).
  • I went back into Visual Studio, did a rebuild and everything went back to work for me.

Reading back over this post I can't believe how easy and simple the fix was. Up until now NuGet had always worked so it never crossed my mind that it would have botched the update. Hopefully this solution will help you out if you see that dreaded error message. Fingers crossed NuGet won't do anything like that again to me.

Dec05

New posts on the way!

Wow, it's been ages since my last post here. I've been super busy with some new exciting work over at Dragnet Systems lately which is coming to an end shortly giving me more time to post some new articles.

Over the coming weeks I'll post some articles explaining how to get the most from MVC3 for real world scenarios. A lot of my work at the moment focuses on MVC3 with Entity Framework Code First, Twitter's BootStrap and jQuery to build amazing web apps for businesses.

I've also been taking the time to learn how to setup my MVC projects to use dependency injection so that my code is much neater and tidier. It took ages to get my head around it initially but it's starting to pay off now which is great.

So there you have it. I'm still here and there will be more new posts very soon :)

How to get the id of a textbox that is using jQuery UI AutoComplete

Recently I had a need for getting the id value of a textbox that was using the jQuery UI AutoComplete plugin. I wanted to pass this id value as a parameter and use it in my Ajax call to my database. To add some extra complexity, my project was using the AutoComplete plugin on numerous textboxes. I didn't want to make a function for each textbox that would be using the AutoComplete so I had to set it up in a slightly generic way.

The trick to getting this working is to wrap your AutoComplete function call around an 'each' loop for the textboxes so that the 'attr' function becomes available to use and allows us to grab the id value we need to use in our Ajax call.

Below is the code I used. As you can see I do a 'each' call for every textbox that uses the 'tb' css class. If you've used the AutoComplete plugin before the rest of this code should be familiar to what you're using. If not, I'd recommend taking a look at the plugin documentation.

$("input:text.tb").each(function () {
        var thisElement = $(this);
        $(this).autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "FetchLocationList/TestPage",
                    data: "{ 'loc': '" + request.term + "', 'textboxid': '" + thisElement.attr("id") + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                value: item.Name,
                                key: item.Code
                            }
                        }))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 3
        });
    });

Fix for CKEditor with MVC3 when using multiple submit buttons

I had an issue this weekend while updating some code from WebForms to MVC3 when using the brilliant CKEditor. Many of our forms display 2 submit buttons - one for publishing and one to save as a draft. When using WebForms there was never any issue with this. I'd just make 2 <asp:Buttons>, add some On_Click handlers and everything was great. With MVC3 I noticed that when using the CKEditor on my forms that I was having to click on the submit buttons twice before the page would submit.

At first I thought this was down to the MVC validation. I removed the validation but the issue still occured. "Ah, it must be the way I handle the postback with the 2 buttons!", I thought, but this didn't seem to work either. I put my button logic and the validation back on my page and removed the class I had put on the textarea of my form to get the CKEditor working and everything worked as expected. The issue was down to the CKEditor and how it was handling multiple submit buttons in a single form. You need to force CKEditor to update the textarea before carrying on with posting the page.

I did some searching on the CKEditor forums and on Google. I came across a piece of javascript that you can add to your webpage to work around this issue (asp.net forums link). Simply add the following to your page and it will resolve the '2 clicks to submit' issue you're having. Note you can add other events to this code if you want but paste and on keyup events should cover you 99% of the time.

$(document).ready(function () {
    for (instance in CKEDITOR.instances) {
        CKEDITOR.instances[instance].on("instanceReady", function () {
            //set keyup event
            this.document.on("keyup", function () { CKEDITOR.instances[instance].updateElement(); });
            //and paste event
            this.document.on("paste", function () { CKEDITOR.instances[instance].updateElement(); });
        });
    }
});

Resolve a HTTP 405 error when linking from a Facebook App

We recently had an issue of a page not loading of us once we integrated it with Facebook. This was a simple html page with nothing special on it so I was a bit confused that it would not load in Facebook for me. We were hosting the webpage on a Windows 2003 server running IIS6. Nothing fancy.

Going to the website in the browser worked great but any request coming from app.facebook.com didn't seem to be getting through. I took a look at FireBug to see what was actually being sent and received when I was calling the website from Facebook and noticed that I was getting back a HTTP 405 error. This was telling me that the calls Facebook were trying to make were not allow on my server.

I did a bit of searching on this and didn't find anything in particular about Facebook and these types of errors. However, I did notice that some people would get these errors on other web apps and the issue was down to 'html' or 'htm' file types not allowed to be called by get or posts from external requests.

To resolve the issue you need to go into IIS and right click on the domain giving you the issue and click on 'Properties'.

Next go to the 'Home Directory' tab and click on the 'Configuration' button.

Click on 'Add' on the 'Mappings' tab that just opened and either browse to the following file or enter it in the textbox:

C:\WINDOWS\System32\inetsrv\asp.dll

Type in ".html" (without the quotes) in the extension box and Limit the calls to just "GET,POST" (without quotes).

Click OK to close the popup window and again to return to the IIS domain list.

When you now view your site through your Facebook app it will appear for you without any issues.

Copy data from one table to another in the same database

There was an issue that cropped up in work today where we noticed there was a table that had been setup incorrectly that had no auto increment column for handling IDs. One of the junior developers found the bug and was telling me that he was going to export out all of the data from the table into an excelsheet, re-import the data into a new table and setup an auto increment column that way.

I guess some people just like making work for themselves :)

All that he had to do was setup a new table, let's call it TableB and give it the same columns as Table A but include an ID column and set it to auto increment and be the primary key. By running the SQL statement below, it was super quick to copy the data from one table to another. Once the data was over he could delete TableA and rename TableB to TableA.

--copy contents from Table A into Table B
Insert TableB (col1, col2, col3)
Select (Col1, Col2, Col3)
From TableA

CoolBlue BlogEngine theme now available over on BlogEngine Gallery

If you have a blog powered by BlogEngine it might be worth heading on over to the Blog Engine Gallery to see what extensions or themes you can download and use on your blog.

I was taking a look over there recently and noticed that my CoolBlue theme is now available for download in the themes section. Simply head on over to http://dnbegallery.org/cms/List/Themes/CoolBlue and click the download link. Setup couldn't be easier. Just pop the CoolBlue folder into your themes folder and you will be able to select the theme from your admin area.

The BlogEngine Gallery is a really great idea - especially now that NuGet is gaining popularity. I'm hoping to have some newer themes available over the summer and I'll definitely be using the BlogEngine Gallery to help get the themes out there for people to download.

how to get Mercury Particle Engine working in XNA 4.0

I've been messing around recently with a really cool particle engine for XNA called Mercury Particle Engine. This engine looks to have some great features and an active development cycle but the documentation can be very lacking.

One query that I keep seeing on their forums is how to actually go about and include it in a simple XNA 4.0 project.

I have attached a simple working project below but if you want to manually setup the particle engine in your existing project here are the steps you'll need to do:

  1. Downloaded Mercury Particle Engine 3.1 for XNA 4.0 (Binaries)
  2. Unblocked the zip file (right click the file and click on Unblock).
  3. Open Visual Studio 2010 and create a new Windows XNA 4 project.
  4. Copy the ProjectMercury.dll and ProjectMercury.ContentPipeline.dll files and place these into your game project in the root level or in a folder called ParticleEngine, whichever you choose.
  5. Check the properties of both files and set the Build Action to 'Content' in Visual Studio.
  6. Add a reference to the file ProjectMercury.dll in your game project.
  7. Add a reference to the files ProjectMercury.dll and ProjectMercury.ContentPipeline.dll in your content project.
  8. Copy the EffectLibrary and Textures from the editor example into your content project (so you now have all the textures and xml files at your disposal).
  9. Do a build and more than likely you will get some errors about some of the Effect XML files. This appeared to be down to 3 of them using the word 'True' instead of 'true' (all lowercase) on certain sections so replaced them and everything should build for you.
  10. Run through the steps from this blog article: http://xnacoding.blogspot.com/2010/07/how-to-mercury-particle-engine.html
  11. You need to make one change to the code from link above. This is where I stumbled initially. The LoadContent method should contain the following code rather than the code from that example:>
// load particle effects
myEffect = Content.Load(@"EffectLibrary\BasicExplosion");
myEffect.LoadContent(this.Content);
myEffect.Initialise();
myRenderer.LoadContent(Content);

When you build and run your project you should now be able to left mouse click and see an explosion particle effect on screen. Pretty cool! If you want to run a different effect simple rename BasicExplosion in the code above to whatever filename is on the XML file in the Effect folder you imported. Just be sure not to include the '.xml' extension.

Click here to download a sample XNA 4 project with Mercury Particle Engine.

Eurl.axd error when redirecting from one asp.net 4 website to another in IIS 6

I recently came across an unusual error when trying to redirect to an asp.net 4 website from another website in IIS6. This came about because a client had 2 domains hosted with us. One domain, lets call it Web1, was setup using asp.net 4 and had a website on it. The other domain, Web2, was just used for emails and contained no website.

The client requested that when users go to the Web2 address they would automatically be brought to the Web1 site. Normally when we do something like this we just setup a redirect rule in II6 on the Web2 domain that redirects to Web1. However, in this instance, once we setup our redirect we were getting an error on the Web1 website saying that it couldn't find a resource: {domain}/eurl.axd/{long number}

After a bit of digging around I came across a good StackOverflow.com query someone had that was similar to my problem. Even though all the answers were not really what I wanted to do I did learn that this was a 'feature' of .net 4, extenionless URLs and IIS6. My solution was to just use a 301 redirect page on the Web2 site and have the user redirected from that page instead of IIS6 itself. This worked like a charm.

So, if you are having this eurl.axd error when redirecting from one website to another and you need to keep extensionless URLs then using a redirect webpage should work for you. Sometimes, the easiest solutions work best!

HTML5 and CSS3 March 2011 round up on Stuff4Designers.com

Stuff4Designers.com HTML5 and CSS3 March 2011 showcase

It's another month so that means it's time for another round up of the latest HTML5 and CSS3 showcase demos from around the web. This month's list includes a stunning lighting effect in Firefox4 by Mozilla, incredible CSS3 boxes using no images and even HTML5 fractals!

The full list is over on Stuff4Designers.com. Check it out and if you are working on any amazing HTML5 or CSS3 examples you like me to include in next month's showcase be sure to le me know in the comments.

View the full HTML5 and CSS3 showcase for 2011 on stuff4designers.com.