2

我想创建一个约定,将名为“Id”的列设置为主键,我一直在查看文档,到目前为止,我发现只能逐个类手动进行,例如:圆顶:

public class ProductMap : DommelEntityMap<TEntity>
{
    public ProductMap()
    {
        Map(p => p.Id).IsKey();
    }
}

我想要更多类似的东西:

    public ConventionMap()
    {
        Properties<int>().Where(p=>p.Name.ToLower()=="id").isKey();
    }

它可以是 dommel 或 dapper 扩展或任何其他,我只是这个实现是流利的。

有什么建议吗?谢谢!

4

1 回答 1

2

Dommel允许您创建自定义IKeyPropertyResolver.

您的实现将如下所示:

public class CustomKeyPropertyResolver : DommelMapper.IKeyPropertyResolver
{
    public PropertyInfo ResolveKeyProperty(Type type)
    {
        return type.GetProperties().Single(p => p.Name.ToLower() == "id");
    }
}

应该在应用程序启动时使用这行代码注册:

DommelMapper.SetKeyPropertyResolver(new CustomKeyPropertyResolver());

为此,您不需要 Dapper.FluentMap(或 Dapper.FluentMap.Dommel)。另请参阅:文档

于 2015-01-14T10:51:49.823 回答