zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
383 stars 85 forks source link

IGNORE is supported? #69

Closed CRC32EX closed 3 years ago

CRC32EX commented 3 years ago

Question

Program.cs

using MySqlConnector;
using Z.Dapper.Plus;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;

namespace DapperPlusTest
{
  [Table("users")]
  class User
  {
    [Column("id")]
    public ulong Id { get; set; }

    [Column("name")]
    public string Name { get; set; }
  }

  class Program
  {
    const string Connstr = "userid=root;password=MYPASSWORD;database=MYDB;Host=127.0.0.1;";

    static MySqlConnection Connect()
    {
      var conn = new MySqlConnection(Connstr);
      conn.Open();
      return conn;
    }

    static void Main()
    {
      using var conn = Connect();

      var users = new List<User>
      {
        new User{ Id = 2, Name = "swan" },
        new User{ Id = 4, Name = "bear" }
      };

      conn.DapperPlusMethodBulkInsertIgnore(users);
    }
  }
}

What i want to do example (SQL)

SELECT * FROM users ;
+--------+----------+
|     id |     name |
+--------+----------+
|      1 | panda    |
|      2 | dog      |
|      3 | cat      |
+--------+----------+

INSERT IGNORE INTO users (id, name) VALUES (2, 'swan'),(4, 'bear') ;
---
Query OK, 1 row affected, 1 warning (0.02 sec)
Records: 2  Duplicates: 1  Warnings: 1

SELECT * FROM users ;
+--------+----------+
|     id |     name |
+--------+----------+
|      1 | panda    |
|      2 | dog      |
|      3 | cat      |
|      4 | bear     |
+--------+----------+

My Environment

OS Windows 10 Pro 2004 64bit
dotnet 3.1.302
MariaDB 10.2.12-MariaDB
Z.Dapper.Plus 3.0.19
MySqlConnector 1.0.1
JonathanMagnan commented 3 years ago

Hello @CRC32EX ,

It's currently not supported. I will look with my developer how we could support it.

Best Regards,

Jon

JonathanMagnan commented 3 years ago

Hello @CRC32EX ,

Here is the code you can use which will do the same behavior:

DapperPlusManager.Entity<EntitySimple>().Table("users").Key(x => x.ID, "id"); 

var users = new List<User>()
{
  new User{ Id = 2, Name = "swan" },
  new User{ Id = 4, Name = "bear" }
};

conn.UseBulkOptions(x => x.InsertIfNotExists = true).BulkInsert(users);

It doesn't use the IGNORE keyword but instead, check if the data already exists or not.

Is that an alternative solution that could work for you?

Best Regards,

Jon

CRC32EX commented 3 years ago

It works well. In my situation IGNORE is not needed anymore. When I really needed IGNORE keyword, i will open this issue again.

In my situation, Key attributes also works well.

Thank you.

using MySqlConnector;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Z.Dapper.Plus;

namespace Test
{
  [Table("users")]
  class User
  {
    [Key]
    [Column("id")]
    public ulong Id { get; set; }

    [Column("name")]
    public string Name { get; set; }
  }

  class Program
  {
    const string Connstr = "userid=root;password=MYPASSWORD;database=MYDB;Host=127.0.0.1;";

    static MySqlConnection Connect()
    {
      var conn = new MySqlConnection(Connstr);
      conn.Open();
      return conn;
    }

    static void Main()
    {
      using var conn = Connect();

      var users = new List<User>()
      {
        new User { Id = 2, Name = "swan" },
        new User { Id = 4, Name = "bear" }
      };

      conn.UseBulkOptions(x => x.InsertIfNotExists = true).BulkInsert(users);
    }
  }
}