using CHMS.App;
using SqlSugar;
using System;
using System.Diagnostics;
using System.Linq;

namespace CHMS.Store
{
    /// <summary>
    /// 程序数据库操作类
    /// </summary>
    public class AppDb
    {
        /// <summary>
        /// 获取程序数据库操作对象
        /// </summary>
        /// <returns></returns>
        public static SqlSugarClient GetDb()
        {
            var db = new SqlSugarClient(
                new ConnectionConfig()
                {
                    ConnectionString = DbConfig.DbString,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.SystemTable
                });
            if (DbConfig.ExecutingLog)
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                 {
                     Debug.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                     Serilog.Log.Logger.Information(
                         sql + Environment.NewLine +
                         db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                 };
            }
            if (DbConfig.ErrorLog)
            {
                db.Aop.OnError = err =>
                {
                    Serilog.Log.Logger.Error(
                        "[数据库错误]" +
                        err.Sql + Environment.NewLine +
                        db.Utilities.SerializeObject(err));
                };
            }

            return db;
        }


        public static SqlSugarClient GetDb(string str)
        {
            var db = new SqlSugarClient(
                new ConnectionConfig()
                {
                    ConnectionString = DbConfig.DbString,
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                });
            if (DbConfig.ExecutingLog)
            {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Debug.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Serilog.Log.Logger.Information(
                        sql + Environment.NewLine +
                        db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                };
            }
            if (DbConfig.ErrorLog)
            {
                db.Aop.OnError = err =>
                {
                    Serilog.Log.Logger.Error(
                        "[数据库错误]" +
                        err.Sql + Environment.NewLine +
                        db.Utilities.SerializeObject(err));
                };
            }

            return db;
        }
    }
}
using CHMS.App;
using CHMS.Entity;
using CHMS.Utility.IO;
using System;
using System.IO;

namespace CHMS.Tools
{
    class Program
    {
        public const string LogTag = ProjectInfo.LogTag + "[" + nameof(Program) + "]";

        static void Main(string[] args)
        {
            while (true)
            {
                ShowMenu();
                var key = Console.ReadLine().Trim();
                Console.WriteLine();
                if (key == "1")
                {
                    GenerateEntity();
                }
                else if (key == "2")
                {
                    GenerateTable();
                }
                else if (key == "0")
                {
                    return;
                }
                else
                {
                    Console.WriteLine("输入有误,请重新输入");
                }
            }
        }


        /// <summary>
        /// 根据实体生成表
        /// </summary>
        private static void GenerateTable() 
        {
            var logTag = LogTag + "[" + nameof(GenerateEntity) + "]";
            DbConfig.DbString = "Data Source=.;Initial Catalog=PSS;User ID=sa;Password=123;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
            var db = Store.AppDb.GetDb("");
            Serilog.Log.Logger.Debug(logTag + "开始测试数据库连接");
            db.Ado.CheckConnection();
            Serilog.Log.Logger.Debug(logTag + "数据库连接成功");

            db.CodeFirst.InitTables<RepBasicOperation>();
        }

        private static void ShowMenu()
        {
            Console.WriteLine(
                "\n\n" +
                "********************\n" +
                "*****   CHMS   *****\n" +
                "********************\n" +
                "** 1.生成Entity\n" +
                "********************\n" +
                "** 2.生成数据库\n" +
                "********************\n" +
                "** 0.退出\n" +
                "********************\n" +
                "请输入菜单编号:");
        }

        private static void GenerateEntity()
        {
            var logTag = LogTag + "[" + nameof(GenerateEntity) + "]";

            try
            {
                DbConfig.DbString = "Data Source=.;Initial Catalog=PSS;User ID=sa;Password=123;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                var db = Store.AppDb.GetDb();
                Serilog.Log.Logger.Debug(logTag + "开始测试数据库连接");
                db.Ado.CheckConnection();
                Serilog.Log.Logger.Debug(logTag + "数据库连接成功");

                var path = AppDomain.CurrentDomain.BaseDirectory + "Entity";
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                db.DbFirst.CreateClassFile(path, "CHMS.Entity");
                Console.WriteLine($"生成完成,请到{path}中查看");
                Serilog.Log.Logger.Debug(logTag + "已生成在:" + path);
                WindowsUtility.OpenPath(path);
            }
            catch (Exception e)
            {
                Serilog.Log.Logger.Information(e, logTag + "意外错误");
                Console.WriteLine(e);
            }
        }
    }
}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐