浅尝NetCore+SqlSugar

一、开发环境:Windows11/Mysql8.0.29-x/Net Core3.1/SqlSugar5.1.4.60

二、appsettings.json 配置

 "ConnectionStrings": {
    "studentBase": "Server=数据库IP;port=端口;database=数据库名称;user=账号;password=密码;charset=utf8;Pooling=True;Max Pool Size=3000;Connect Timeout=60;Connection Lifetime=3600;sslMode=None;AllowLoadLocalInfile=true;"
  }

三、Startup 注入SqlSugarContext

// 注入 Startup
services.AddSugarDbContext<StudentSugarContext>(Configuration.GetConnectionString("studentBase"));

四、实现扩展方法:AddSugarDbContext

     /// <summary>
        /// 添加Sugar上下文
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="services"></param>
        /// <param name="connectionString">数据库链接字符串</param>
        /// <param name="enableSqlLog">是否启用Sql日志输出</param>
        public static void AddSugarDbContext<T>(this IServiceCollection services, string connectionString, bool enableSqlLog = false) where T : SugarContextBase
        {
            services.AddScoped<T>();
            var name = typeof(T).Name;
            var options = services.AddOptions<ConnectionConfig>(name);
            services.Configure<ConnectionConfig>(name, op =>
            {
                op.ConfigId = name;
                op.ConnectionString = connectionString;
                op.DbType = DbType.MySql;
                op.IsAutoCloseConnection = true;
                if (enableSqlLog)
                {
                    op.AopEvents = new AopEvents
                    {
                        OnLogExecuting = (sql, pars) =>
                        {
                            Console.WriteLine($"{string.Join(", ", pars?.Select(it => it.ParameterName + "=" + it.Value))}");
                            Console.WriteLine(sql);
                        }
                    };
                }
            });
        }

五、 实现上下文Context:StudentSugarContext


 public abstract class SugarContextBase
    {
        public readonly SqlSugarClient SugarClient;
        public SugarContextBase(IOptionsSnapshot<ConnectionConfig> namedOptionsAccessor)
        {
            var options = namedOptionsAccessor.Get(this.GetType().Name);
            SugarClient = new SqlSugarClient(options);
        }
    }

    public class StudentSugarContext : SugarContextBase
    {
        public StudentSugarContext(IOptionsSnapshot<ConnectionConfig> namedOptionsAccessor) : base(namedOptionsAccessor)
        {
        }
    }

六、业务写入(注入上下文)

[SugarTable("student")]
public class Student
{
    public Student() { }
    [SugarColumn(IsPrimaryKey = true)]
    public string Id { get; set; }
    public string Name { get; set; }
    public string Bo_Id { get; set; }
}
   private readonly StudentSugarContext _studentSugarContext;

    public StudentController(StudentSugarContext _studentSugarContext)
    {
        this._studentSugarContext = _studentSugarContext;
    }


    //
public async void WritBusindessInfo(){
    #region 测试数据
    var data = new List<Student>();
    for (int i = 0; i < 1000000; i++)
    {
        data.Add(new Student { Name = $"Test{i}", Bo_Id = $"{i}" });
    }
    #endregion
    using var transaction = _studentSugarContext.SugarClient.UseTran();
    await _studentSugarContext.SugarClient.Fastest<Student>().BulkCopyAsync(data);
    transaction.CommitTran();
}

七、检查数据是否写入成功

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netcore是一个开发框架,用于构建基于.Net平台的应用程序。它提供了丰富的工具和功能,使开发人员能够快速高效地开发Web应用程序和服务。Netcore通过使用C#语言和ASP.Net Core框架,提供了强大的开发环境。 React是一个JavaScript库,使用组件化思想来构建用户界面。React具有高性能和可重用的组件,使开发人员能够有效地构建复杂的前端应用程序。通过使用虚拟DOM(Virtual DOM)的机制,React能够快速地更新页面,并提供可扩展的功能。 Netcore和React可以很好地配合使用,以构建现代化的Web应用程序。开发人员可以使用Netcore提供的工具和框架来构建强大的后端服务,同时使用React来构建用户界面。通过将两者结合起来,开发人员可以实现前后端的分离,提高开发速度和效率。 使用Netcore和React的好处有很多。首先,Netcore具有很好的性能和可伸缩性,适用于处理大量请求和数据的应用程序。React的虚拟DOM机制可以提高页面的渲染速度,提供更好的用户体验。 其次,Netcore和React都有很好的社区支持和文档资源,开发人员可以很容易地找到所需的帮助和支持。此外,Netcore和React都是开源的,有很多开源项目和库可以使用,加快开发速度。 最后,Netcore和React的结合还可以提供更好的开发体验和可维护性。开发人员可以使用React的组件化思想来构建可重用的UI组件,同时使用Netcore的工具来管理项目的依赖和构建过程。 总而言之,Netcore和React是两个强大的开发工具,结合使用可以实现高效、可扩展和可维护的Web应用程序。无论是开发大型企业应用还是小型的个人项目,Netcore和React都是不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值