GlobalVar.cs 1.41 KB
using FreeSql;
using FreeSql.SqlServer;
using System.Data.SqlClient;

namespace HH.Mes.Tool.Utils
{
    internal static class GlobalVar
    {
        public static IFreeSql FreeSql = null!;

        private static string _connectionString = null!;

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public static string ConnectionString => _connectionString;

        /// <summary>
        /// 设置数据库连接字符串
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        public static void SetConnectionString(string connectionString)
        {
            _connectionString = connectionString;
            FreeSql = new FreeSqlBuilder()
            //.UseConnectionString(DataType.SqlServer, GetConnectionString())
            .UseConnectionFactory(DataType.SqlServer, () => new SqlConnection(ConnectionString), typeof(SqlServerProvider<>))
            .UseMonitorCommand(cmd => cmd.CommandTimeout = 60, null)
            //监听SQL语句
            //.UseMonitorCommand(cmd =>
            //{
            //    if (cmd.CommandText.ToLower().Contains("delete"))
            //        Console.WriteLine($"Sql:{cmd.CommandText}");
            //})
            .UseAutoSyncStructure(false) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
            .Build();
        }
    }
}