不要使用dapper扩展更新Update方法上的某个属性

我知道我可以使用纯粹的dapper来构建我的更新字符串,只包含我想要更新的属性(白名单).

我想要的是保持dapper扩展的ORM风格:

con.Update<Person>(person);

我想要一些人的属性不更新(黑名单)

如何从运行.Update扩展方法的更新中排除属性?

你是否知道更精致的.Update扩展在小巧玲珑的风格? (然后我不必

写下来 😉

最佳答案 只需定义一个继承自ClassMapper< Person>的PersonMapper.并使用Ignore()映射要排除的列.

请参阅下面的示例(来源:github tests for Dapper Extensions),其中排除了电话属性(IEnumerable< Phone>电话).

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();
        }
    }
}
点赞