插入实体列表,怎么处理重复问题? 返回

SqlSugar 老数据
3 2023
该叫什么 simadi 发布于2021/8/29
悬赏:5 飞吻

比如库中已有2的记录,现在传入实体里有2的记录,怎么忽略掉这个重复了并插入其他的。

https://www.donet5.com/home/Doc?typeId=1208

我按照这里的处理的。


这个表mid是主键

用如下代码,会提示 Need primary key or WhereColumn

更改为

 3.png


后还是同样提示,求指教,消息

热忱回答3

  •  我这个用例是OK的,你可以新建个控制台验证一下

              //建表
             Db.CodeFirst.InitTables<MicroBlog>();
    
             var x= Db.Storageable(new List<MicroBlog>()
                {
                    new MicroBlog(){ Mid="1" },
                    new MicroBlog(){ Mid="2" }
                })
                 .SplitInsert(it=>!it.Any())
                 .WhereColumns(it=>it.Mid).ToStorage();
    
                x.AsInsertable.ExecuteCommand();
            }
            public class MicroBlog
            {
                //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]//如果是主键,此处必须指定,否则会引发InSingle(id)方法异常。
                public string Mid { get; set; }
                //public int id { get; set; }
                [SqlSugar.SugarColumn(IsNullable = true)]
                public string uid { get; set; }
                [SqlSugar.SugarColumn(IsNullable = true)]
                public string nick { get; set; }
                [SqlSugar.SugarColumn(IsNullable = true)]
                public DateTime SendTime { get; set; }
                [SqlSugar.SugarColumn(IsNullable = true)]
                public string content { get; set; }
                [SqlSugar.SugarColumn(IsNullable = true)]
                public string ForwardHtml { get; set; }
                [SqlSugar.SugarColumn(IsNullable =true)]
                public string MediaHtml { get; set; }
                //public DateTime CreatedAt { get; set; }
            }


    0 回复
  •  public SqlSugarClient Db= new SqlSugarClient(new ConnectionConfig()
            {
                DbType = DbType.MySql,
                ConnectionString = Config.ConnectionString,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, p) =>
                    {
                        Console.WriteLine(sql);
                        Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
                    }
                }
            });


    0 回复
  • 知道什么原因了,你自个肯定封装了  T , 这个T要加上  Where:class,new() 泛型 约束 ,不然会进错重载

    0 回复