FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

mysql 分表时执行ExecuteMySqlBulkCopy 报错

Open MoDao1990217 opened this issue 11 months ago • 2 comments

问题描述及重现代码:

mysql 按时间进行分表,执行ExecuteMySqlBulkCopy 出现错误,错误信息:“分表字段值 "0001-01-01 00:00:00" 不能小于 "2024-01-01 00:00:00 "

经调试,疑似异常出现在以下位置。

8379fd60d9af3f55e5a2fd6adb24109a

fbcf14289f54b86540597b21150d47bc

f513eb1f69d05f732afacbd10c64de37

在ToSql函数中,重新创建了一个data,在调用getlnsertSal函数时,内部将这个list赋值给_source了,导致在TableRulelnvoke函数获表表名时,出现了异常。

后续发现,当不给分表时间字段默认值时,会出现此故障。而给了默认值后,会有新有问题。#1754


using FreeSql.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

using System.Text;
using System.Text.Json;
using System.Xml.Linq;



var services = new ServiceCollection();

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, @"Data Source=127.0.0.1;Port=3306;User ID=root;Password=123456; Initial Catalog=myTest;Charset=utf8;Allow User Variables=True; SslMode=none;Min pool size=100;AllowLoadLocalInfile=true;")
    .UseMonitorCommand(cmd => Console.WriteLine($"Sql:{cmd.CommandText}"))//监听SQL语句
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库,FreeSql不会扫描程序集,只有CRUD时才会生成表。
    .Build();

fsql.Aop.ConfigEntityProperty += (s, e) =>
{
    if (e.Property.PropertyType.IsEnum)
    {
        e.ModifyResult.MapType = typeof(int);
    }
    if (e.Property.PropertyType == typeof(DateTime) || e.Property.PropertyType == typeof(DateTime?))
    {
        e.ModifyResult.DbType = "DATETIME";
    }
};

var data = new List<Test1> {
    new Test1 { Name = "张三", Age = 20 ,Created=DateTime.Parse("2024-02-21 18:21:44")} ,
    new Test1{Name="李四",Age=23,Created=DateTime.Now}
};

var dic = data.GroupBy(c=>c.Created.ToString("yyyyMM")).ToDictionary(c=>c.Key,b=>b);

foreach (var item in dic)
{
    try
    {
        fsql.InsertOrUpdate<Test1>()
            .AsTable($"test_{item.Key}")
            .SetSource(item.Value)
            .ExecuteMySqlBulkCopy();
    }
    catch (Exception e)
    {
        throw;
    }
}





[Table(Name = "test_{yyyyMM}", AsTable = $"{nameof(Created)}=2024-1-1(1 month)")]
public class Test1
{
    [Column(IsIdentity =true,IsPrimary =true)]
    public int Id { get; set; }
    public string Name { get; set;  }
    public int Age { get; set; }

    public DateTime Created { get; set; }

}

数据库版本

mysql5.7

安装的Nuget包

freesql.provider.mysqlconnector.3.2.810

.net framework/. net core? 及具体版本

.net core 6

MoDao1990217 avatar Mar 04 '24 04:03 MoDao1990217

最近比较忙,这类问题使用 .ExecuteAffrows 解决。

.ExecuteAffrows 和 .ExecuteBulkCopy 的选择:

操作数据较少时(例如500行记录以内),使用 Affrows

2881099 avatar Mar 04 '24 11:03 2881099

最近比较忙,这类问题使用 .ExecuteAffrows 解决。

.ExecuteAffrows 和 .ExecuteBulkCopy 的选择:

操作数据较少时(例如500行记录以内),使用 Affrows

嗯,都是导入的大文件,上百万的。现在使用了其他方式,文件分批处理的。

MoDao1990217 avatar Mar 05 '24 04:03 MoDao1990217