NHibernate: Reasons for overriding Equals and GetHashCode
Date : March 29 2020, 07:55 AM
seems to work fine Overloading the Equals and GetHashCode methods is important if you are working with multiple sessions, detached entities, stateless sessions or collections (see Sixto Saez's answer for an example!). In the same session scope identity map will ensure that you only have a single instance of the same entity. However, there is the possibility of comparing an entity with a proxy of the same entity (see below).
|
Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability
Tag : chash , By : Mare Astra
Date : March 29 2020, 07:55 AM
Any of those help MSDN actually doesn't say "don't overload Equals et al for mutable types". It used to say that, but now it says:
|
EF Hashset collection - Overriding Equals and GetHashCode
Date : March 29 2020, 07:55 AM
Does that help Why should I use Hashset? You shouldn't. The uniqueness of a many-to-many row will be enforced at the database level so long as it's mapped properly. There is little to no benefit in enforcing it at the application level. public class UserChartQuery
{
public int UserId { get; set; }
public int ChartQueryId { get; set; }
public virtual User User { get; set; }
public virtual ChartQuery ChartQuery { get; set; }
protected bool Equals(UserChartQuery other)
{
return UserId == other.UserId && ChartQueryId == other.ChartQueryId;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((UserChartQuery) obj);
}
public override int GetHashCode()
{
unchecked
{
return (UserId*397) ^ ChartQueryId;
}
}
}
public class ChartQuery
{
public int ChartQueryId { get; set; }
public virtual ICollection<User> Users { get; set; }
...more...
}
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<ChartQuery> SavedChartQueries { get; set; }
...more...
}
builder.Entity<ChartQuery>()
.HasMany(cq => ucq.Users)
.WithMany(u => u.SavedChartQueries);
|
What is the best way to test for object equality - without overriding Equals & GetHashCode, or implementing IEquatab
Tag : chash , By : user109285
Date : March 29 2020, 07:55 AM
|
Overriding GetHashCode and Equals on a class because it's used in a dictionary
Tag : chash , By : foxthrot
Date : March 29 2020, 07:55 AM
|