Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using hashset in entity framework

I want to know what is the difference between creating classes with or without using "hashset" in constructor.

Using code first approach (4.3) one can creat models like this:

public class Blog  {      public int Id { get; set; }      public string Title { get; set; }      public string BloggerName { get; set;}      public virtual ICollection<Post> Posts { get; set; }   }  public class Post  {     public int Id { get; set; }     public string Title { get; set; }     public DateTime DateCreated { get; set; }     public string Content { get; set; }     public int BlogId { get; set; }     public ICollection<Comment> Comments { get; set; }  } 

or can create models like this :

public class Customer {     public Customer()     {         BrokerageAccounts = new HashSet<BrokerageAccount>();     }     public int Id { get; set; }     public string FirstName { get; set; }     public ICollection<BrokerageAccount> BrokerageAccounts { get; set; } }  public class BrokerageAccount {      public int Id { get; set; }     public string AccountNumber { get; set; }     public int CustomerId { get; set; }  } 

What is hashset doing here?

should i use hashset in the first two models also?

is there any article which shows the application of hashset?

like image 809
Amir Jalali Avatar asked Jun 21 '12 04:06

Amir Jalali


People also ask

What is HashSet entity framework?

The HashSet is mostly useful in situations where insertion and removal times are very important, such as when processing data. It is also extremely useful for comparing sets of data (again when processing) using operations like intersect, except, and union. In any other situation, the cons generally outweigh the pros.

What is HashSet in c#?

In C#, HashSet is an unordered collection of unique elements. This collection is introduced in . NET 3.5. It supports the implementation of sets and uses the hash table for storage. This collection is of the generic type collection and it is defined under System.

What is ICollection in Entity Framework?

ICollection represents a type of collection. specifying a collection as an ICollection allows you to use any type of collection in your code that implements the ICollection interface.

Why use Entity Framework?

The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.


1 Answers

Generally speaking, it is best to use the collection that best expresses your intentions. If you do not specifically intend to use the HashSet's unique characteristics, I would not use it.

It is unordered and does not support lookups by index. Furthermore, it is not as well suited for sequential reads as other collections, and the fact that it allows you to add the same item multiple times without creating duplicates is only useful if you have a reason to use it for that. If that is not your intention, it can hide misbehaving code and make problems difficult to isolate.

The HashSet is mostly useful in situations where insertion and removal times are very important, such as when processing data. It is also extremely useful for comparing sets of data (again when processing) using operations like intersect, except, and union. In any other situation, the cons generally outweigh the pros.

Consider that when working with blog posts, inserts and removes are quite rare compared to reads, and you generally want to read the data in a specific order, anyway. That is more or less the exact opposite of what the HashSet is good at. It is highly doubtful that you would ever intend to add the same post twice, for any reason, and I see no reason why you would use set-based operations on posts in a class like that.

like image 85
Daniel Avatar answered Sep 22 '22 23:09

Daniel