mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-21 02:02:50 +08:00

- 增加 无参数化命令执行,可配置全局 ICodeFirst.IsNoneCommandParameter、或临时 IInsert/IUpdate.NoneParameter() 便于调试; - 关闭 自动同步结构功能,避免线上环境误操作; - 优化 IInsert 批量插入容易导致 values 过多、或参数化过多的问题,5个数据库均已优化;
30 lines
1.2 KiB
C#
30 lines
1.2 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
|
|
public static class FreeUtil {
|
|
|
|
private static DateTime dt1970 = new DateTime(1970, 1, 1);
|
|
private static ThreadLocal<Random> rnd = new ThreadLocal<Random>(() => new Random());
|
|
private static readonly int __staticMachine = ((0x00ffffff & Environment.MachineName.GetHashCode()) +
|
|
#if NETSTANDARD1_5 || NETSTANDARD1_6
|
|
1
|
|
#else
|
|
AppDomain.CurrentDomain.Id
|
|
#endif
|
|
) & 0x00ffffff;
|
|
private static readonly int __staticPid = Process.GetCurrentProcess().Id;
|
|
private static int __staticIncrement = rnd.Value.Next();
|
|
/// <summary>
|
|
/// 生成类似Mongodb的ObjectId有序、不重复Guid
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static Guid NewMongodbId() {
|
|
var now = DateTime.Now;
|
|
var uninxtime = (int)now.Subtract(dt1970).TotalSeconds;
|
|
int increment = Interlocked.Increment(ref __staticIncrement) & 0x00ffffff;
|
|
var rand = rnd.Value.Next(0, int.MaxValue);
|
|
var guid = $"{uninxtime.ToString("x8").PadLeft(8, '0')}{__staticMachine.ToString("x8").PadLeft(8, '0').Substring(2, 6)}{__staticPid.ToString("x8").PadLeft(8, '0').Substring(6, 2)}{increment.ToString("x8").PadLeft(8, '0')}{rand.ToString("x8").PadLeft(8, '0')}";
|
|
return Guid.Parse(guid);
|
|
}
|
|
} |