【问题标题】:How to perform strict mapping on Dapper如何在 Dapper 上执行严格的映射
【发布时间】:2016-08-17 19:04:26
【问题描述】:

我正在使用 dapper 将 SQL 结果集直接映射到我的 C# 对象,一切正常。

我正在使用这样的语句来进行映射

var result = connection.Query( "sp_select", );

但此语句似乎并未强制执行类字段与从数据库返回的列之间的精确映射。意思是,当 POCO 上的字段在结果集中不存在时,它不会失败。

我确实喜欢实现松散并且不强制执行任何限制权的事实,但是 dapper 是否有任何功能可以让我在认为映射成功之前从结果集中要求某些字段?

【问题讨论】:

    标签: c# .net dapper


    【解决方案1】:

    你也可以试试Dapper-Extensions

    这是一个例子:

    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
    }
    
    [TestFixture]
    public class DapperExtensions
    {
        private SqlConnection _connection;
    
        [SetUp]
        public void Init()
        {
            _connection = new SqlConnection(@"Data Source=.\sqlexpress;Integrated Security=true; Initial Catalog=mydb");
            _connection.Open();
    
            _connection.Execute("create table Person(Id int not null, FirstName varchar(100) not null, LastName varchar(100) not null)");
            _connection.Execute("insert into Person(Id, FirstName, LastName) values (1, 'Bill', 'Gates')");
        }
    
        [TearDown]
        public void Teardown()
        {
            _connection.Execute("drop table Person");
            _connection.Close();
        }
    
        [Test]
        public void Test()
        {
            var result = _connection.Get<Person>(1);
        }
    }
    

    由于 Person 表中缺少 Address 列,测试将失败。

    您也可以忽略带有Custom Maps 的列:

    public class PersonMapper : ClassMapper<Person>
    {
        public PersonMapper()
        {
            Map(x => x.Address).Ignore();
            AutoMap();
        }
    }
    

    【讨论】:

      【解决方案2】:

      您无法通过属性或标志“自动”执行此操作。您可以关注此open Github issue 了解更多背景信息。

      这可以由您手动通过在 select 子句中自己映射每个属性来完成,尽管此时您已经失去了很多功能和易用性使用 Dapper。

      var result = connection.Query<MyClass>("sp_select")
                             .Select(x => 
                             {
                                 // manually map each property and verify
                                 // that the data is returned
                             });
      

      【讨论】:

      • 感谢您指出 github 上的未解决问题,我确信有人已经在某个时候要求此功能......我只是在搜索中没有找到正确的措辞。他们拒绝立即添加该功能是有道理的。
      猜你喜欢
      • 2020-06-15
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多