How to Configure a Self Referencing Entity in Code First

Recently I was working on a project that required a categorization option. I wanted the user to have the option to create categories and as many subcategories as they wanted. To realize this I created a category model that was self referencing:

It’s important that you don’t forget the Foreignkey data annotation because otherwise you will get an error like this:

Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

Further make sure that ParentId is nullable because it makes no sense if every category has a parent if it was even possible.

Alternative

Instead of using data annotations you can also use Fluent API to create a self referencing model. When you choose for this approach add the following code to your DbContext:

More information

Floating point and Culture issues

Recently I was working on a ASP.NET MVC application where users could add budgets. The budgets were saved as floats. The problem was that some users used a (.) as decimal and others a (,). It depends on which culture setting your application is running if ModelState.isValid is true or false in such case. The culture setting in which your application runs depends on the system on which it is running. This is not a desired behaviour. Its better to specify the culture in the application itself and force users to use a (.) or (,). To run an ASP.NET MVC application in a specific culture setting you can add the following code to your Global.asax file:

More information:
http://stackoverflow.com/questions/19905694/c-sharp-mvc-4-model-changing-floating-point

URL Rewriting in EPiServer 6

Today I was working on a project that uses EPiServer version 6. As you probably know EPiServer 6 is still using webforms, not MVC. For SEO optimalisation we needed to rewrite incoming URL’s to make them uniform. For example all these URL’s should go to the same page

/en/Some-Interesting-Page
/Some-Interesting-Page
/en/some-interesting-page

For SEO it is important that there is only one valid URL. Other URL’s should throw a statuscode 301 and redirect to the correct url. For this I needed to do some URL rewriting in EPiServer.

Continue reading