使用鉴别器得相关批量插入异常 返回

SqlSugar 沟通中
8 756
该叫什么 hantzl 发布于2025/9/29
悬赏:0 飞吻

描述:启用鉴别器的表单条插入正常,ExecuteCommandAsync批量插入报错,EnableDataAop().BulkCopyAsync 无法自动给鉴别器赋值(仅测试了sqllite和mysql)

sqlsugar版本号 :5.1.4.205


测试代码片段(基于教程中的测试demo)



using SqlSugar;

SqlSugarHelper.Db.DbMaintenance.CreateDatabase();//创建库

SqlSugarHelper.Db.CodeFirst.InitTables<Animal, Dog, Cat>();


var cats = new List<Cat>() { new() { M = "1" }, new() { M = "2" }, };

var dogs = new List<Dog>() { new() { W = "1" }, new() { W = "2" }, };


try

{

    await SqlSugarHelper.Db.Insertable(cats).ExecuteCommandAsync();

}

catch (Exception e) { Console.WriteLine("ex:" + e.Message); }

try

{

    await SqlSugarHelper.Db.Insertable(dogs).ExecuteCommandIdentityIntoEntityAsync();

}

catch (Exception e) { Console.WriteLine("ex:" + e.Message); }

try

{

    await SqlSugarHelper.Db.Fastest<Cat>().EnableDataAop().BulkCopyAsync(cats);

}

catch (Exception e) { Console.WriteLine("ex:" + e.Message); }

try

{

    await SqlSugarHelper.Db.Fastest<Dog>().EnableDataAop().BulkCopyAsync(dogs);

}

catch (Exception e) { Console.WriteLine("ex:" + e.Message); }


var cat = await SqlSugarHelper.Db.Queryable<Cat>().FirstAsync(p => true);


var count = await SqlSugarHelper.Db.Queryable<Animal>().CountAsync(p => true);


Console.WriteLine($"执行完成, dataCount = {count}!");

Console.ReadKey();


[SugarTable("Animal", IsDisabledDelete = true)]

public class Animal {

    [SugarColumn(IsIdentity = true, IsPrimaryKey = true)] 

    public int Id { get; set; }

}

[SugarTable("Animal", Discrimator = "Type:1", IsDisabledDelete = true)]

public class Dog : Animal { public string W { get; set; } }

[SugarTable("Animal", Discrimator = "Type:2", IsDisabledDelete = true)]

public class Cat : Animal { public string M { get; set; } }


public class SqlSugarHelper //不能是泛型类

{

    public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()

    {

        ConnectionString = "datasource=test.db",//连接符字串

        DbType = DbType.Sqlite,//数据库类型

        IsAutoCloseConnection = true //不设成true要手动close

    },

  db => {

      db.Aop.OnError = (exp) => {

          var sqlStr = UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, exp.Sql, exp.Parametres as SugarParameter[]);

          Console.WriteLine("aop:" + exp.Message);

          Console.WriteLine("sql" + sqlStr);

      };

  });

}


热忱回答8

  • fate sta fate sta VIP0
    2025/9/29

    处理中

    0 回复
  • fate sta fate sta VIP0
    2025/9/30

    SqlSugarCore 5.1.4.206-preview06

    过五分钟ExecuteCommandAsync已修复。 

    勾预览

    0 回复
  • hantzl hantzl VIP0
    2025/9/30

    好的,非常感谢!请问 BulkCopyAsync 后期有优化计划么?我这边刚把源码拿下来尝试了下,发现 对 SqlSugar-master\Src\Asp.NetCore2\SqlSugar\Abstract\FastestProvider\Private.cs 下的    private DataTable ToDdateTable(List<T> datas) {} 方法进行调整可能可以解决这个问题(暂未深入了解此项目且自身能力有限,代码并未进行严格测试),希望能对您的后续优化有所帮助


    image.png


            private Dictionary<string, string> GetDiscrimator()

            {

                var dict = new Dictionary<string, string>();

                if (entityInfo.Discrimator.HasValue())

                {

                    Check.ExceptionEasy(!Regex.IsMatch(entityInfo.Discrimator, @"^(?:\w+:\w+)(?:,\w+:\w+)*$"), "The format should be type:cat for this type, and if there are multiple, it can be FieldName:cat,FieldName2:dog ", "格式错误应该是type:cat这种格式,如果是多个可以FieldName:cat,FieldName2:dog,不要有空格");

                    var array = entityInfo.Discrimator.Split(',');

                    foreach (var disItem in array)

                    {

                        var name = disItem.Split(':').First();

                        var value = disItem.Split(':').Last();

                        dict.TryAdd(name, value);

                    }

                }


                return dict;

            }


    0 回复
  • fate sta fate sta VIP0
    2025/9/30

    可以pull代码给我

    0 回复
  • fate sta fate sta VIP0
    2025/9/30
    0 回复
  • hantzl hantzl VIP0
    2025/9/30

    好的,https://gitee.com/dotnetchina/SqlSugar/pulls/96 ,方便的时候麻烦帮忙看下,谢谢

    0 回复
  • fate sta fate sta VIP0
    2025/9/30

    SqlSugarCore 5.1.4.206-preview08


    过五分钟后安装

    0 回复
  • hantzl hantzl VIP0
    2025/10/15

    嗯嗯,谢谢,请问正式版预计发布周期大概是多久呢?

    0 回复