Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dapper.Contrib Update ignores mapping #2

Open
xalikoutis opened this issue May 9, 2016 · 20 comments
Open

Dapper.Contrib Update ignores mapping #2

xalikoutis opened this issue May 9, 2016 · 20 comments

Comments

@xalikoutis
Copy link

Hi,

some fields in my table have underscores like full_name so i did
DefaultTypeMap.MatchNamesWithUnderscores = true;
which works just fine when i retrieve entity with Get<> but throws "Invalid column name 'FullName'."
when i update

my model

public class SmUser{
        public int Id { get; set; } 
        public string Name { get; set; }    
        public string FullName { get; set; }   
        public string Email { get; set; }        
    }

Whet i run Get it works just fine, the property FullName is populated with data
var user = cn.Get<SmUser>(id);

but when i update the entity

user.Email = user.Email + "z";
cn.Update(user);

i get
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
"Invalid column name 'FullName'."

and the sql query
update smuser set [Name] = @Name, [FullName] = @FullName, [Email] = @Email where [Id] = @Id

"FullName" is the entity property, "full_name" is the db field
using dapper 1.42

@xalikoutis
Copy link
Author

xalikoutis commented May 10, 2016

This feature is not supported . In Dapper.Contrib.SqlMapperExtensions.cs are fetched only the properties from the entity type and constructs directly the update statement.

DefaultTypeMap.MatchNamesWithUnderscores or any implementation of SqlMapper.ITypeMap is not taken in account.

I ll give it a try myself
Thanks

@stevo-knievo
Copy link

Same for Insert<>

Dapper: 1.50.0-rc2 and Dapper.Contrib: 1.50.0-beta8

@pdcmoreira
Copy link

I've created a pull request that implements the mapping for Insert and Update the last week, but I don't know if the maintainer have seen it yet.

@stevo-knievo
Copy link

Nice work! I'm happy to test it as soon as gets integrated.

@secretwep
Copy link

Wow, I was really counting on this working. The db schema I'm using still has some old tables in it with underscored columns and the classes all utilize CamelCase property names. I went to test some updates and that didn't go so well.

@dr-tariq-n-ahmad
Copy link

Is this really not addressed 4 years later?

@koshovyi
Copy link

MatchNamesWithUnderscores = true still now working in insert case :(

@agarzas
Copy link

agarzas commented Dec 13, 2020

+1

1 similar comment
@blai30
Copy link

blai30 commented Feb 14, 2021

+1

@BunnyMan1
Copy link

BunnyMan1 commented May 1, 2021

My take on this to update a Class with lots of properties:

var query = $@"UPDATE {tableName} SET ";
            foreach (var prop in objDb.GetType().GetProperties())
            {
                if (prop.Name == "Id" || prop.Name == "CreatedAt" || prop.Name == "Application" || prop.Name == "Platform")
                    continue; // The properties that I don't want to update

                query = query + $@"{prop.Name.ToSnakeCase()} = @{prop.Name}, ";
            }
            query = query.Remove(query.Length - 2, 2); // Removing last comma and space
            query = query + $@" WHERE id = @Id";

            await _connection.ExecuteAsync(query, objDb);

This logic can be made into a reusable function with parameters as necessary.

Example: UpdateInDB(connection, objDb, tableName)

To snake_case from PascalCase converter as:

public static string ToSnakeCase(this string str)
        {
            Regex pattern = new Regex(@"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+");
            return string.Join("_", pattern.Matches(str)).ToLower();
        }

@NickCraver NickCraver transferred this issue from DapperLib/Dapper May 8, 2021
@JerryLiew
Copy link

Is this really not addressed 5 years later?

@dominicjordan
Copy link

Is this repo still maintained?

@mrerhuo
Copy link

mrerhuo commented Jun 13, 2022

Is this really not addressed 6 years later?

@biju-ps
Copy link

biju-ps commented Jul 15, 2022

+1

@PandaWood
Copy link

PandaWood commented Feb 12, 2023

Wow, I was really counting on this working. The db schema I'm using still has some old tables in it with underscored columns and the classes all utilize CamelCase property names. I went to test some updates and that didn't go so well.

This is almost obligatory to have underscores in Postgres - given the way it handles case and quotes. Hence I don't quite understand how anyone using Postgres can use Dapper.Contrib... I can't
I guess we can map every field to every underscored column and then use it... but surely the MatchNamesWithUnderscores value should be respected?

@H-pun
Copy link

H-pun commented Apr 29, 2023

Is this really not addressed 7 years later?

@dom-scotpac
Copy link

For anyone looking for an alternative, I went with FastCrud after years of using Dapper.Contrib. Luckily it was a "drop in" replacement that didn't require any code changes (for me at least) and supported the [Column] attribute that I had in place for my schema migrations. Hopefully Dapper.Contrib can add support for this in the future.

@pdcmoreira
Copy link

My PR was closed at the time because they wanted to support a [Column] attribute instead. They opened discussions about it for both v2 and a future v3, but that was back in 2017.
At this point, I don't know if it will ever get anywhere.

@lanyeeee
Copy link

this problem not fixed until today :(

@Unysno
Copy link

Unysno commented Aug 18, 2023

do someone have alternative?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests