Closed liuyl1992 closed 4 years ago
What do you mean by false random generator? It's not crypto security grade, but it's good enough for most purposes. To be honest, I'd advise against any of those:
case "Int16":
return (TKey)(object)Random.Next(1, short.MaxValue);
case "Int32":
return (TKey)(object)Random.Next(1, int.MaxValue);
case "Int64":
return (TKey)(object)(Random.NextLong(1, long.MaxValue));
For generating primary keys in MongoDB, it's just for retrocompatibility for the EF stuff that is relying of Identity columns and incremental Ids. If you still want to use integers for Id's you can always rely on the heavy duty RNG Crypto generator for "real randomness".
https://github.com/alexandre-spieser/mongodb-generic-repository/blob/f0af32adcad2b58a435cdad72d4e041331030b8e/MongoDbGenericRepository/Utils/IdGenerator.cs#L11