【问题标题】:NPOCO: Why is my custom Mapper not calledNPOCO:为什么我的自定义映射器没有被调用
【发布时间】:2016-07-12 07:36:58
【问题描述】:

在我的一个实体中,我需要一个特殊的映射,它将分隔字符串(来自数据库)转换为字典。 而不是使用一些虚拟属性,我认为更优雅的方式是自定义映射器,如here

所述

我的问题是从未调用过映射器中的任何方法,并且 NPOCO 尝试将我的 db-string 转换为显然失败的字典。

也许我误解了映射器或遗漏了一些东西。但无法解释为什么它不起作用。

我的映射(简化)

public class MyEntityMap : Map<MyEntity>
{
    public MyEntity()
    {
        Columns(x =>
            {   
              x.Column(c => c.SpecialColumn).WithName("SpecialColumn");
            }, true);
    }
}

我的映射器(没做太多)

public class MyMapper : DefaultMapper
{
    public override Func<object, object> GetFromDbConverter(MemberInfo destMemberInfo, Type sourceType)
    {
        return base.GetFromDbConverter(destMemberInfo, sourceType);
    }

    public override Func<object, object> GetFromDbConverter(Type destType, Type sourceType)
    {
        return base.GetFromDbConverter(destType, sourceType);
    }

    public override bool MapMemberToColumn(MemberInfo pi, ref string columnName, ref bool resultColumn)
    {
        return base.MapMemberToColumn(pi, ref columnName, ref resultColumn);
    }

    public override void GetTableInfo(Type t, TableInfo ti)
    {
        base.GetTableInfo(t, ti);
    }
}

我的数据层(构造函数)

protected SimpleDepot()            
    {
        //SQL Server / MySQL
        if (!mappingInitialized)
        {
            NPocoDatabaseFactory.Setup("connectionstring");
            mappingInitialized = true;
        }

        Database = NPocoDatabaseFactory.DbFactory.GetDatabase();
        Database.Mapper = new MyMapper();

        PocoData = Database.PocoDataFactory.ForType(typeof(TEntity));
    }

所以至少在我看来一切都井井有条。 任何我想念的想法。 我目前正在使用 NPOCO 2.9.103,但我正在检查是否可以更新到 v3

【问题讨论】:

    标签: c# npoco


    【解决方案1】:

    好的,所以我刚刚升级到 NPoco v3,它比想象的要顺畅(只需更改一些小代码部分以匹配重大更改)

    请参阅 https://github.com/schotime/NPoco/wiki/Version-3 了解重大更改。

    无论如何:在 v3 中,我的映射器会被调用。 也许这是 v2 中的错误?

    如果您在使用 v2 时遇到同样的问题,请考虑升级到 v3(存在可能会或可能不会影响您的重大更改)

    我目前使用的是 v3.4.1

    【讨论】:

      猜你喜欢
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多