Dapper & MS Access - Read works, Write doesn't

后端 未结 2 1331
庸人自扰
庸人自扰 2020-12-16 19:58

Let\'s start by getting this out of the way: I\'m stuck using an MS Access DB and I can\'t change it.

This works fine:

using (OleDbConnection conn =          


        
2条回答
  •  遥遥无期
    2020-12-16 20:51

    After some digging, I was able to find a cause:

    Below is CreateParamInfoGenerator delegate from dapper's SqlMapper:

        public static Action CreateParamInfoGenerator(Identity identity)
        {
    
            // code above here
            IEnumerable props = type.GetProperties().OrderBy(p => p.Name); 
    

    The props is your unanimous param which gets re-ordered by OrderBy(p => p.Name), which moves city upfront.

    new { firstName = "John", city = "SomeCity", lastName = "Smith" }
    

    Props is then being added to the IDbCommand Parameters where the order is important.

    If I comment out OrderBy() clause, then everything works.

    I also tested DynamicParameters and intentionally re-ordered the attributes to move city upfront:

            var parameters = new DynamicParameters();
            parameters.Add("city", "SomeCity");
            parameters.Add("firstName", "John");
            parameters.Add("lastName", "Smith");
    
            var result = dbConnection.Query(
              "update Students set FirstName = @firstName, City = @city where LastName = @lastName",
              parameters
            );
    

    The above did not work as well, so the order of attributes is the reason!

    I guess you can modify your local copy of SqlMapper for now and remove OrderBy() and wait for an official verdict from Marc...

    Hope this helps.

提交回复
热议问题