Tag Archives: C#

Relationships in Entity Framework Code First

In this article I will describe a common mapping problem with relationships in Entity Framework Code First. I once had this problem as well and got the answer from StackOverflow (if interested, see my question here).

Consider the following models:

And the following (psuedo)code to save a ClassA:

Continue reading Relationships in Entity Framework Code First

MSBuild task to compile LESS files

When you are using LESS to create dynamic style sheets the performance of your website will increase when you compile those files to CSS before uploading the webapplication to a server. In this post I’ll describe how to create a build task for MSBuild that will compile all LESS files in a directory. I use this task in my deployment environment for web applications. It is part of a series of tasks that build the application, copies all the files to a deployment directory and does some post processing (compressing JavaScript, images, etc.). Of course it is possible to compile LESS files dynamically using a HttpHandler as well, for more information visit dotless.org.

Continue reading MSBuild task to compile LESS files

Possible solution for NHibernate many-to-many criteria

In one of my .NET projects I’m using the NHibernate library for object-relational mapping. I’m mainly using the ICriteria interface to fetch data from the database. Unfortunately I ran into a function that got really complicated; how to query a many-to-many relationship. For example, I have a table containing posts and a table containing tags. The post datamodel contains a set with tags so in my mapping it’s a many-to-many relationship. I want my query to return all posts tagged with one or more specific tags. On this forum I found a solution. I’m not sure if this is the perfect solution, so feel free to suggest a better one.
Continue reading Possible solution for NHibernate many-to-many criteria

Using log4net to show NHibernate SQL in Visual Studio

I’m currently developing a web application in ASP.NET MVC and NHibernate. Since the application was very slow at some points I wanted to know which SQL queries NHibernate was executing. Since the log4net library was already included in the application I searched for a solution to output the SQL queries to the console in Visual Studio 2010 using log4net. I found the solution on this wiki and this blog post.

Continue reading Using log4net to show NHibernate SQL in Visual Studio

Type conversion in C#

Type conversion (or typecasting) is the technique to change an entity from one data type into another. In general there are two types of type conversion; implicit and explicit conversion. This post tells about type conversion in C#.NET and the performance issues.

Implicit conversion

Implicit conversion (or coercion) is automatic type conversion by the compiler. In C# it does not require a special syntax because the type conversion is safe and no data will be lost. In the following example the value of an integer is stored in a long. This is safe because an integer is 4 bytes (on a 32 bit computer), and a long is 8 bytes. So every value that fits in an integer fits in a long. For a complete conversion table see this page.

It is also possible to cast a derived class to a base class without using a special syntax.
Continue reading Type conversion in C#

Using Rhino Mocks to mock a void function

In some of my web applications I use Rhino Mocks to create mock objects. Usually the code has a record section where the expected calls are recorded, and a playback section where the testing is done. In the following code snippet the DoSomething() function calls a void function which is defined in the interface of _myMock. The problem is this void function is not expected and the NUnit test run will fail:

Continue reading Using Rhino Mocks to mock a void function