Click here to Skip to main content
13,042,421 members (113,103 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 19 Dec 2011

Entity Framework and T4: Generating GetHashCode() for your entities

, 19 Dec 2011
Rate this:
Please Sign up or sign in to vote.
A simple Entity Framework T4 template that generates GetHashCode() method

This is a simple Entity Framework T4 template that generates GetHashCode() method.

Table of Contents

GetHashCode() Overview

A hash code is a numeric value that is used to identify an object during equality testing, and it can also serve as an index for an object in a collection, such as a List, Dictionary or HashTable.

The problem is that the default .NET implementation of GetHashCode() does not guarantee unique values for different values, so you should override this method and provide your own implementation.

Some rules for implementing GetHashCode() are:

  • If two objects compare as equal, the GetHashCode method for each object must return the same value. However, if two objects do not compare as equal, the GetHashCode methods for the two object do not have to return different values.
  • The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state.
  • For the best performance, a hash function must generate a random distribution for all input.
  • Implementations of the GetHashCode method must not result in circular references (it can lead to a StackOverflowException).
  • Implementations of the GetHashCode method must not throw exceptions.

This is a short overview of GetHashCode() method, taken from MSDN. You can read more at Object.GetHashCode Method.

Implementing GetHashCode()

I found a great post on StackOverflow on this topic. Jon Skeet has provided a good and simple implementation. Basically, you need to use prime numbers like 17, 23, 29, 31 in the hash code calculation. I decided to include also the type of the object in the calculation, because two objects with identical properties/values can return the same hash code.

public class Organisation
	public int Id { get; set; }
	public string Name { get; set; }
	public string Country { get; set; }

	public override int GetHashCode()
			int multiplier = 31;
			int hash = GetType().GetHashCode();

			hash = hash * multiplier + Id.GetHashCode();
			hash = hash * multiplier + 
				(Name == null ? 0 : Name.GetHashCode());
			hash = hash * multiplier + 
				(Country == null ? 0 : Country.GetHashCode());

			return hash;

Using T4 templates to Generate GetHashCode()

T4 is a code generator built right into Visual Studio. You can generate any text file using T4 templates: C#, JavaScript, HTML, XML and many others. If you’ve never heard about it, this is a good place to start:

I’ve modified the ADO.NET C# POCO Entity Generator template to generate GetHashCode() method for each entity in the model. Feel free to download the demo project (VS2010).



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rui Jarimba
Software Developer (Senior)
Italy Italy
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.

I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).

Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.

I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.

Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).

You may also be interested in...


Comments and Discussions

GeneralMy vote of 4 Pin
Loona7029-May-12 4:15
memberLoona7029-May-12 4:15 
GeneralRe: My vote of 4 Pin
Rui Jarimba11-Jul-12 9:35
memberRui Jarimba11-Jul-12 9:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Dec 2011
Article Copyright 2011 by Rui Jarimba
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid