mikependon / RepoDB

A hybrid ORM library for .NET.
Apache License 2.0
1.7k stars 125 forks source link

Bug: Update with Dictionary<string,object> fails using MySql/MariaDb #832

Open LucaGabi opened 3 years ago

LucaGabi commented 3 years ago

Bug Description

Update with Dictionary<string,object> fails using MySql/MariaDb

Exception Message:

RepoDb.Exceptions.KeyFieldNotFoundException
  HResult=0x80131500
  Message=No primary key and identify found at type 'System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]'.
  Source=RepoDb
  StackTrace:
   at RepoDb.DbConnectionExtension.GetAndGuardPrimaryKeyOrIdentityKey(Type entityType, IEnumerable`1 dbFields)
   at RepoDb.DbConnectionExtension.GetAndGuardPrimaryKeyOrIdentityKey(IDbConnection connection, String tableName, IDbTransaction transaction, Type entity)
   at RepoDb.DbConnectionExtension.Update[TEntity](IDbConnection connection, String tableName, TEntity entity, IEnumerable`1 fields, String hints, Nullable`1 commandTimeout, IDbTransaction transaction, ITrace trace, IStatementBuilder statementBuilder)
   at ConsoleApp6.Program.Main(String[] args) in C:\Users\Luca\source\repos\ConsoleApp6\Program.cs:line 27

Schema and Model:

Please share to us the schema of the table (not actual) that could help us replicate the issue if necessary.

CREATE TABLE eFOpoUmRYW.Person (
  Id INT(11) NOT NULL AUTO_INCREMENT,
  Name VARCHAR(150) DEFAULT NULL,
  Age INT(11) DEFAULT NULL,
  CAT DATETIME DEFAULT NULL,
  PRIMARY KEY (Id)
)
ENGINE = INNODB,
CHARACTER SET utf8,
COLLATE utf8_unicode_ci;

And also the model that corresponds the schema.

    public class Person
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime CAT { get; set; }
    }

Library Version:

Example: RepoDb v1.12.X and RepoDb.SqlServer v1.1.X

LucaGabi commented 3 years ago
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using RepoDb;
using System;
using System.Collections.Generic;

namespace ConsoleApp6
{

    public class Person
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime CAT { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MySqlBootstrap.Initialize();

            var cns = "********************";
            using (var connection = new MySqlConnection(cns))
            {
                var updatedRows = connection.Update("Person",
                    entity: new Dictionary<string, object> {
                        { "Id",1 },
                        { "Age",42 }
                    });

                var ppls = connection.QueryAll<Person>();

                foreach (var p in ppls)
                {
                    Console.WriteLine(JsonConvert.SerializeObject(p));
                }
            }

            Console.ReadKey();
            Console.WriteLine("Hello World!");
        }
    }
}
SergerGood commented 3 years ago

Working with latest code