Skip to content

Table mapping for UniqueColumn uses unstable GetHashCode() method #1769

Closed
@Scooletz

Description

@Scooletz

The snippet below presents the way how Table class generates UniqueColumn name. This value is used in several code paths, like CreateForeignKey and/or generating an unique column name itself.

public string UniqueColumnString(IEnumerable iterator, string referencedEntityName)
{
// NH Different implementation (NH-1399)
int result = 37;
if (referencedEntityName != null)
{
result ^= referencedEntityName.GetHashCode();
}
foreach (object o in iterator)
{
result ^= o.GetHashCode();
}
return (name.GetHashCode().ToString("X") + result.GetHashCode().ToString("X"));

string.GetHashCode is not a stable hashing function, according to the documentation

The hash code itself is not guaranteed to be stable. Hash codes for identical strings can differ across versions of the .NET Framework and across platforms (such as 32-bit and 64-bit) for a single version of the .NET Framework. In some cases, they can even differ by application domain. This implies two subsequent runs of the same program may return different hash codes.

Creating the script then, can have a different outcome when run in two different environments, like .NET Core 2.1 and net461 or even across 32/64 bits.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions