3Sixty Systems
Back to 3Sixty  |   Products & services   |   News  |   Partners  |   About us  |   Case studies  |   Careers  |   Contact us

Using ELMAH with ASP.NET MVC3

by Neil Milner-Harris 25. April 2011 23:26

I've just being implementing ELMAH in a new ASP.NET MVC3 project, it really is a must have for any ASP.NET project. Turns out that there are a some key changes to the exception handling process in MVC3 that cause a bit of grief for users of ELMAH. Whilst poking around for some information I stumbled across a really great blog series (there's 5 posts in total) about implementing ELMAH in MVC3.

I recommend anyone who is interested in using ELMAH, and there really is no excuse for not being interested, should check out Joel's blog here as it is a great step by step guide and overview.

A Simple Entity Framework 4 Problem

by Neil Milner-Harris 5. April 2011 00:15

Right a simple problem this, with a fairly simple solution, but not entirely intuitive or obvious.

Supposing we have a data context called context which is a model of a SQL database which includes a table called Country.

Now we want to create a dropdown for those countries, based on a visibility flag and ordered alpahbetically, simple right?

var countries = from c in context.Countries
                            where c.Visible
                            orderby c.Name
                            select new SelectListItem
                                {
                                    Value = c.Id.ToString(),
                                    Text = c.Name
                                };

Well no. Doing that will garner you the exception:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression

This is because the entire Linq statement must be translated to a store expression to be executed against the underlying data store, ToString() is not a supported method for this. (On a personal note I have no idea why ToString() is not supported against a SQLServer data store, I hope they fix that up in a future release.)

The solution is not particularly elegant but it is incredibly simple.

 var countries = from c in context.Countries
                            where c.Visible
                            orderby c.Name
                            select c;
 
 var countriesList = from c in countries.AsEnumerable<Country>()
                     select new SelectListItem
                          {
                              Value = c.Id.ToString(),
                              Text = c.Name
                          };

In this particular example (which could be contracted a little, it just makes more sense formatted this way) we set up the Linq query to get the data we want in the order we want. Then we use that query in a Linq to objects statement beneath. The important point to note is the use of the AsEnumerable<>() method. This forces the original Linq query to actually execute and return the list of relevant countries. This then frees the second Linq query from the Linq to entities restrictions and turns it into a simple Linq to objects query.

Not particularly elegant, but it is simple and it works.

It is worth noting that any method that causes the first Linq to execute would have sufficed in the second query, AsEnumerable just makes the most sense in this particular example.

 

 

 

I've got a really annoying VS 2010 bug

by Neil Milner-Harris 3. September 2010 21:05

Update: My colleague has found a fix for this issue. Uninstall the WOVSS Default Browser extension and reinstalling it fixes the issue.

I have a VS2010 solution that contains 4 Database Projects. All of a sudden in the middle of the day yesterday, having already done some deployments that morning, VS crashed and restarted when I tried to deploy one of the DB projects.

So I reloaded the solution and tried again and it crashed again. Joy!

Bringing up the properties dialog for the project renders the error message:

 

An error occurred trying to load the project properties window.  Close the window and try again.

Cannot evaluate the item metadata "%(FullPath)". The item metadata "%(FullPath)" cannot be applied to the path "obj\Debug|Any CPU\RenatusDev.dbschema". Illegal characters in path.  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

 

So one quick google later reveals the following post:

http://blog.projectsoftware.ro/2009/09/database-projects-error-an-error-occurred-trying
-to-load-the-project-properties-window-close-the-window-and-try-again/

Essentially the solution to this issue is to run:

devenv.exe /setup

devenv.exe /resetuserdata

That actually works and fixes the issue, but then you have to spend ages reinstalling all of your extensions and recreating all of your settings.

Fine as a one off but it has just happened for a second time in two days so now I'm looking for any other solutions that don't involve resetting all the VS user data.

Please comment if you've got one....

Month List

Powered by BlogEngine.NET 1.6.0.0