DbContextFactory.cs 878 Bytes
using FreeSql;
using HHECS.DAQHandle.DataAccess;
using System.Configuration;

namespace HHECS.DAQHandle
{
    internal static class DbContextFactory
    {
        private static readonly string connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;

        private static IFreeSql _freeSql = new FreeSqlBuilder()
            .UseConnectionString(DataType.SqlServer, connectionString)
            //.UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
            .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
            .Build();

        public static IFreeSql CreateFreeSql() => _freeSql;

        public static DataContext CreateDbContext()
        {
            return new DataContext(_freeSql);
        }
    }
}