Updated packages to 2.0.1 and added support for primary keys of type long.
This commit is contained in:
@@ -14,10 +14,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<!--<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />-->
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.1" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
|
||||
<PackageReference Include="MongoDbGenericRepository" Version="1.3.0" />
|
||||
<PackageReference Include="MongoDbGenericRepository" Version="1.3.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace AspNetCore.Identity.MongoDbCore.Extensions
|
||||
{
|
||||
// Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/6651656/5103354
|
||||
/// <summary>
|
||||
/// Extensions for the random number generator <see cref="Random"/>
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from min (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="min">The inclusive minimum bound</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
|
||||
public static long NextLong(this Random random, long min, long max)
|
||||
{
|
||||
if (max <= min)
|
||||
throw new ArgumentOutOfRangeException("max", "max must be > min!");
|
||||
|
||||
//Working with ulong so that modulo works correctly with values > long.MaxValue
|
||||
ulong uRange = (ulong)(max - min);
|
||||
|
||||
//Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419
|
||||
//for more information.
|
||||
//In the worst case, the expected number of calls is 2 (though usually it's
|
||||
//much closer to 1) so this loop doesn't really hurt performance at all.
|
||||
ulong ulongRand;
|
||||
do
|
||||
{
|
||||
byte[] buf = new byte[8];
|
||||
random.NextBytes(buf);
|
||||
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
|
||||
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
|
||||
|
||||
return (long)(ulongRand % uRange) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from 0 (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than 0</param>
|
||||
public static long NextLong(this Random random, long max)
|
||||
{
|
||||
return random.NextLong(0, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long over all possible values of long (except long.MaxValue, similar to
|
||||
/// random.Next())
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
public static long NextLong(this Random random)
|
||||
{
|
||||
return random.NextLong(long.MinValue, long.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using AspNetCore.Identity.MongoDbCore.Interfaces;
|
||||
using AspNetCore.Identity.MongoDbCore.Extensions;
|
||||
using AspNetCore.Identity.MongoDbCore.Interfaces;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
@@ -67,9 +68,15 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
||||
case "Guid":
|
||||
Id = (TKey)(object)guidValue;
|
||||
break;
|
||||
case "Int16":
|
||||
Id = (TKey)(object)GlobalVariables.Random.Next(1, short.MaxValue);
|
||||
break;
|
||||
case "Int32":
|
||||
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
||||
break;
|
||||
case "Int64":
|
||||
Id = (TKey)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue));
|
||||
break;
|
||||
case "String":
|
||||
Id = (TKey)(object)guidValue.ToString();
|
||||
break;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using AspNetCore.Identity.MongoDbCore.Interfaces;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using AspNetCore.Identity.MongoDbCore.Extensions;
|
||||
|
||||
namespace AspNetCore.Identity.MongoDbCore.Models
|
||||
{
|
||||
@@ -373,9 +374,15 @@ namespace AspNetCore.Identity.MongoDbCore.Models
|
||||
case "Guid":
|
||||
Id = (TKey)(object)guidValue;
|
||||
break;
|
||||
case "Int16":
|
||||
Id = (TKey)(object)GlobalVariables.Random.Next(1, short.MaxValue);
|
||||
break;
|
||||
case "Int32":
|
||||
Id = (TKey)(object)GlobalVariables.Random.Next(1, int.MaxValue);
|
||||
break;
|
||||
case "Int64":
|
||||
Id = (TKey)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue));
|
||||
break;
|
||||
case "String":
|
||||
Id = (TKey)(object)guidValue.ToString();
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user