GlobalVar.cs 1.32 KB
using FreeSql;
using FreeSql.SqlServer;
using System.Configuration;
using System.Data.SqlClient;

namespace HHECS.DAQHandle
{
    internal static class GlobalVar
    {
        public static IFreeSql FreeSql => new FreeSqlBuilder()
            //.UseConnectionString(DataType.SqlServer, connectionString)
            .UseConnectionFactory(DataType.SqlServer, () => new SqlConnection(GetConnectionString()), typeof(SqlServerProvider<>))
            //监听SQL语句
            //.UseMonitorCommand(cmd =>
            //{
            //    if (cmd.CommandText.ToLower().Contains("delete"))
            //        Console.WriteLine($"Sql:{cmd.CommandText}");
            //})
            .UseAutoSyncStructure(false) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
            .Build();

        private static string GetConnectionString()
        {
            _ = bool.TryParse(ConfigurationManager.AppSettings["IsProductionEnvironment"], out var isProductionEnvironment);
            if (isProductionEnvironment)
            {
                return ConfigurationManager.ConnectionStrings["Production"].ConnectionString;
            }
            else
            {
                return ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
            }
        }
    }
}