/// <summary> /// Determine whether this entity is equal to another entity. /// </summary> /// <param name="other">The entity to compare to this entity when determining equality.</param> /// <returns>True if the other entity is not null, neither entity is transient, and both /// entities share the same Id value.</returns> public virtual bool Equals(DbEntityWithId <TId> other) { // instance is never equal to null if (other == null) { return(false); } // when references are equal, they are the same object if (ReferenceEquals(this, other)) { return(true); } // when either object is transient or the id's are not equal, return false if (IsTransient(this) || IsTransient(other) || !Equals(Id, other.Id)) { return(false); } // when the id's are equal and neither object is transient // return true when one can be cast to the other // because this entity could be generated by a proxy var otherType = other.GetUnproxiedType(); var thisType = GetUnproxiedType(); return(thisType.IsAssignableFrom(otherType) || otherType.IsAssignableFrom(thisType)); }
private static bool IsTransient(DbEntityWithId <TId> obj) { // an object is transient when its id is the default (null for strings or 0 for numbers) return(Equals(obj.Id, default(TId))); }