5

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 ;-)

Elisabeth
  • 20,496
  • 52
  • 200
  • 321

3 Answers3

1

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();
        }
    }
}
Shiva
  • 20,575
  • 14
  • 82
  • 112
  • 1
    Your code would ignore my property on Insert/Update. I just want to ignore a certain property only for UPDATE. Your code will not help here. – Elisabeth Nov 06 '13 at 21:39
1

In the ClassMapper for the relevant type, do this:

public MyTableMapper()
{
    Table("MyTable");
    Map(f => f.MyCalculatedField).ReadOnly();
}
tomfanning
  • 9,552
  • 4
  • 50
  • 78
1

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.

Konstantin
  • 339
  • 2
  • 15