5

我使用下划线字符命名我的 MYSQL 表和列:

this_is_a_table应映射到:ThisIsATable

this_is_a_column应映射到:ThisIsAColumn

如果我设置,Dapper 可以处理此映射:

DefaultTypeMap.MatchNamesWithUnderscores = true;

有没有办法在 Dapper-Extensions 中启用它,以便它自动映射 undescore?

4

1 回答 1

3

这非常简单,您只需要创建一个自定义映射。这是一个例子:

创建表:

create table hello_world
(
    Id int not null,
    Value_Column varchar(max)
)

测试:

public class World
{
    public int Id { get; set; }
    public string Value { get; set; }
}

public class WorldCustomMapper : ClassMapper<World>
{
    public WorldCustomMapper()
    {
        base.Table("hello_world");
        Map(f => f.Id).Column("Id");
        Map(f => f.Value).Column("Value_Column");
    }
}

[TestFixture]
public class Class1
{
    [Test]
    public void TestMappping()
    {
        var conn = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=mydb");
        conn.Open();

        var record = new World
        {
            Id = 1,
            Value = "Hi"
        };

        conn.Insert(record);

        var result = conn.Get<World>(1);

        Assert.That(result.Value, Is.EqualTo("Hi"));

    }
}
于 2016-02-09T23:47:17.740 回答