批量合并出错 返回

SqlSugar 沟通中
4 317
 public async Task<int> MergeAsync<T>(T data) where T : class, new()
 {     return await Task.Run( () =>
     {         return  SugarClient.Storageable(data).ExecuteCommand();
     });

 }

这个方法,我批量合并的时候,如果主键完全重复,会报错。

public partial class PerformanceIndicator:ObservableObject{
       
    [SugarColumn(IsPrimaryKey = true)]    public int Layer { get; set; }
    [SugarColumn(IsPrimaryKey = true)]    public int Channel { get; set; }
    [SugarColumn(IsPrimaryKey = true)]    public string Action { get; set; }
    [SugarColumn(IsPrimaryKey = true)]    public DateTime FinishTime { get; set; }
    [ObservableProperty]    public double ellipticity380;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush ellipticity380Color = Brushes.Black;
    [ObservableProperty]    private double nearX;
    [ObservableProperty]    private double nearY;
    [ObservableProperty]    private double farX;
    [ObservableProperty]    private double farY;

    [ObservableProperty]    private double iL;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush iLColor = Brushes.Black;

    [property: SugarColumn(IsIgnore = true)]
    [ObservableProperty]    private double centerX380;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private double centerY380;
    [ObservableProperty]    private double currentPax;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush currentPaxColor =Brushes.Black;
    [ObservableProperty]    private double currentPay;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush currentPayColor  = Brushes.Black;
    [ObservableProperty]    private double currentOffsetX;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush currentOffsetXColor = Brushes.Black;
    [ObservableProperty]    private double currentOffsetY;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush currentOffsetYColor = Brushes.Black;
    [ObservableProperty]    private double diameter50;
    [ObservableProperty]    private double diameter200;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush diameter200Color = Brushes.Black;
    [ObservableProperty]    private double diameter380;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush diameter380Color = Brushes.Black;
    [ObservableProperty]    private double finalPax;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush finalPaxColor  = Brushes.Black;
    [ObservableProperty]    private double finalPay;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush finalPayColor  = Brushes.Black;
    [ObservableProperty]    private double finalOffsetX;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush finalOffsetXColor  = Brushes.Black;
    [ObservableProperty]    private double finalOffsetY;
    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private SolidColorBrush finalOffsetYColor  = Brushes.Black;

    [ObservableProperty]
    [property: SugarColumn(IsIgnore = true)]    private double power ;    public PerformanceIndicator Copy()
    {        PerformanceIndicator copy = new PerformanceIndicator();        // 1. 复制主键/标识字段 (通常作为业务唯一标识)
        copy.Layer = this.Layer;        copy.Channel = this.Channel;        copy.Action = this.Action;        copy.FinishTime = this.FinishTime;        // 2. 复制所有数值型数据字段
        copy.Ellipticity380 = this.Ellipticity380;        copy.NearX = this.NearX;        copy.NearY = this.NearY;        copy.FarX = this.FarX;        copy.FarY = this.FarY;        copy.CurrentPax = this.CurrentPax;        copy.CurrentPay = this.CurrentPay;        copy.CurrentOffsetX = this.CurrentOffsetX;        copy.CurrentOffsetY = this.CurrentOffsetY;        copy.IL = this.IL;        copy.CenterX380 = this.CenterX380;        copy.CenterY380 = this.CenterY380;        copy.Diameter50 = this.Diameter50;        copy.Diameter200 = this.Diameter200;        copy.Diameter380 = this.Diameter380;        copy.FinalPax = this.FinalPax;        copy.FinalPay = this.FinalPay;        copy.FinalOffsetX = this.FinalOffsetX;        copy.FinalOffsetY = this.FinalOffsetY;            

        // 4. 复制 UI 颜色字段 (关键:使用 Clone 实现深拷贝)
        // 注意:SolidColorBrush 是引用类型,直接赋值会导致两个对象共享同一个画刷实例。
        // 修改其中一个的颜色会影响另一个,因此必须调用 .Clone()。
        copy.Ellipticity380Color = this.Ellipticity380Color.Clone();        copy.CurrentPaxColor = this.CurrentPaxColor.Clone();        copy.CurrentPayColor = this.CurrentPayColor.Clone();        copy.CurrentOffsetXColor = this.CurrentOffsetXColor.Clone();        copy.CurrentOffsetYColor = this.CurrentOffsetYColor.Clone();        copy.ILColor = this.ILColor.Clone();        copy.Diameter200Color = this.Diameter200Color.Clone();        copy.Diameter380Color = this.Diameter380Color.Clone();        copy.FinalPaxColor = this.FinalPaxColor.Clone();        copy.FinalPayColor = this.FinalPayColor.Clone();        copy.FinalOffsetXColor = this.FinalOffsetXColor.Clone();        copy.FinalOffsetYColor = this.FinalOffsetYColor.Clone();        copy.Power = this.Power;    
        return copy;

    }    public  bool IsInvalid()
    {        return double.IsNaN(this.Ellipticity380) ||            double.IsNaN(this.Diameter380) ||            double.IsNaN(this.NearX) ||            double.IsNaN(this.NearY) ||            double.IsNaN(this.FarX) ||            double.IsNaN(this.FarY);
    }
}



热忱回答(4)

  • 如果使用

     public async Task<int> MergeListAsync<T>(List<T> data) where T : class, new()
     {     return await Task.Run( () =>
         {         return  SugarClient.Storageable(data).ExecuteCommand();
         });
    
     }

    那么在合并的时候,时间小数只能保留到毫秒

    0 回复
  • 主键不能重复

    0 回复
  • @fate sta:Storageable方法不是如果存在数据就更新,不存在就插入吗?

    0 回复
  • @fate sta:你好,是怎么回事呢,这条函数不是不存在则插入,存在则更新吗

    0 回复