如何在 Dapper 上进行严格映射
How to perform strict mapping on Dapper
我正在使用 dapper 将 SQL 结果集直接映射到我的 C# 对象,一切正常。
我正在使用这样的语句来进行映射
var result = connection.Query< MyClass >( "sp_select", );
但此语句似乎并未强制执行 class 字段与从数据库返回的列之间的精确映射。意思是,当结果集中不存在 POCO 上的字段时,它不会失败。
我确实喜欢这样的事实,即实现是松散的并且不强制执行 bat 的任何限制权,但是 dapper 是否有任何功能允许我在确定映射之前从结果集中请求某些字段成功了吗?
您无法通过属性或标志强制执行此 "automagically"。您可以关注此 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
});
你也可以试试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 table 中缺少地址列,测试将失败。
您也可以忽略带有 Custom Maps 的列:
public class PersonMapper : ClassMapper<Person>
{
public PersonMapper()
{
Map(x => x.Address).Ignore();
AutoMap();
}
}
我正在使用 dapper 将 SQL 结果集直接映射到我的 C# 对象,一切正常。
我正在使用这样的语句来进行映射
var result = connection.Query< MyClass >( "sp_select", );
但此语句似乎并未强制执行 class 字段与从数据库返回的列之间的精确映射。意思是,当结果集中不存在 POCO 上的字段时,它不会失败。
我确实喜欢这样的事实,即实现是松散的并且不强制执行 bat 的任何限制权,但是 dapper 是否有任何功能允许我在确定映射之前从结果集中请求某些字段成功了吗?
您无法通过属性或标志强制执行此 "automagically"。您可以关注此 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
});
你也可以试试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 table 中缺少地址列,测试将失败。
您也可以忽略带有 Custom Maps 的列:
public class PersonMapper : ClassMapper<Person>
{
public PersonMapper()
{
Map(x => x.Address).Ignore();
AutoMap();
}
}