5
votes

I know I can use the pure dapper to build my update string with only the properties I want to update (whitelist).

What I want is to keep the ORM style of dapper extensions:

con.Update<Person>(person);

I want that some properties of person are not updated (blacklist)

How can I exlude properties from being updated running the .Update extension method?

Do you maybe know of a better .Update extension in the dapper style? (Then I dont have to

write it ;-)

3
How did you resolve this? It's 2017 and I have this same problem - Rob L

3 Answers

1
votes

Just define a PersonMapper that inherits from ClassMapper<Person> and use Ignore() to Map the columns you want to exclude.

See example below (source: github tests for Dapper Extensions ) where the Phones property ( IEnumerable<Phone> Phones ) is excluded.

using System;
using System.Collections.Generic;
using DapperExtensions.Mapper;

namespace DapperExtensions.Test.Data
{
    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateCreated { get; set; }
        public bool Active { get; set; }
        public IEnumerable<Phone> Phones { get; private set; }
    }

    public class Phone
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

    public class PersonMapper : ClassMapper<Person>
    {
        public PersonMapper()
        {
            Table("Person");
            Map(m => m.Phones).Ignore();
            AutoMap();
        }
    }
}
1
votes

In the ClassMapper for the relevant type, do this:

public MyTableMapper()
{
    Table("MyTable");
    Map(f => f.MyCalculatedField).ReadOnly();
}
1
votes

You can try using a separate class for that (for example, a descendant of Person or a wrapper around it with a separate mapper that ignores the property).

But that is way too ugly to be a good solution.

DapperExtensions currently are way too straightforward for anything except explicit CRUD tasks. I had to develop a custom mapper just to add System.ComponentModel annotations support, which seems ridiculous for 2015.

But adding a new UpdatePartial extension method looks like a nice idea.