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

标签 dapper dapper-extensions

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

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

con.Update<Person>(person);

我希望人的某些属性不被更新(黑名单)

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

你可能知道一个更好的 .Update 扩展名的 dapper 风格吗? (那我就不用

写下来 ;-)

最佳答案

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

请参见下面的示例(来源:github tests for Dapper Extensions),其中 Phones属性 ( IEnumerable<Phone> Phones ) 被排除在外。

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

关于dapper - 不要使用 dapper 扩展更新 Update 方法上的某个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823010/

相关文章:

c# - 集合中的 Dapper Multipmapping 集合

C#、Dapper、SQL Server 和连接池

c# - Dapper.net 2.0 无法抛出异常

c# - 如何在使用 DapperExtensions 映射时指定 Schema?

c# - 使用 DapperExtensions 比较两个字段的谓词

c# - 如何在 Dapper 中使用 Char 和 VarBinary?

c# - 如何动态地为 Dapper 查询创建参数

c# - Dapper 使用单数表名

c# - 如何使用 Dapper-Extensions 自动绑定(bind)下划线表/列名称?