AutoMapper将IEnumerable映射到DataReader问题 [英] AutoMapper Mapping IEnumerable to DataReader Issue

查看:125
本文介绍了AutoMapper将IEnumerable映射到DataReader问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AutoMapper来使用数据读取器,其代码如下所述
http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/

I am using AutoMapper to datareader using code as discussed below http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/

我看到了

1)相同的代码和相同的datareader有时会将价值带回到dto结果集,有时却无法。
2)我有一个来自数据库的ID值,即100、200。当它映射到整数类型的DTO时,此100更改为一个较大的值(例如234343211)。

1) Same code with same datareader at times brings value back to the dto result set and at times doesnot. 2) I have an ID value coming from database as 100, 200. When it maps to the DTO that is of type integer this 100 is changed to a large value (like 234343211).

关于我为什么看到这种不一致的任何想法。我应该在(reader.Read())时使用标准的
吗?并停止使用automapper?

Any ideas on why am I seeing this inconsitency. Should I be using the standard while (reader.Read())? and stop using automapper?

推荐答案

我遇到了同样的问题。当您的源类型和目标类型不完全相同时,似乎会发生这种情况。

I ran into this same problem. It seems to occur when your source type and destination type are not exactly the same.

对于我来说,我有一个SQL Server表,其中的ID字段为INT类型。该值已映射到具有long(Int64)类型的属性的类。这将导致期望值100映射到类似668386727769314912的位置。更改表架构以使ID为BIGINT后,值始终会正确映射。

In my case, I had a SQL Server table with an ID field that was of type INT. The value was being mapped to a class with a property that was of type long (Int64). This would result in the expected value of 100 getting mapped to something like 668386727769314912. After changing the table schema so that ID is a BIGINT, the values always mapped correctly.

I建议您仔细查看源类型和目标类型,以确保它们完全相同。显然,您希望隐式进行的转换(例如从Int32到Int64)会导致问题。

I would recommend looking closely at the source type and destination type to ensure they are exactly the same. Apparently, conversions that you would expect to work implicitly (like Int32 to Int64) can cause problems.

这里有一个例子可以重现该问题:

Here is an example that will reproduce the problem:

public class DataMapperIssue
{
    public class Person
    {
        public long id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
    }

    public static void run()
    {
        var table = new DataTable();

        table.Columns.Add("id", typeof(int));
        table.Columns.Add("first_name", typeof(string));
        table.Columns.Add("last_name", typeof(string));

        table.Rows.Add(100, "Jeff", "Barnes");
        table.Rows.Add(101, "George", "Costanza");
        table.Rows.Add(102, "Stewie", "Griffin");
        table.Rows.Add(103, "Stan", "Marsh");
        table.Rows.Add(104, "Eric", "Cartman");

        AutoMapper.Mapper.Reset();
        AutoMapper.Mapper.CreateMap<IDataReader, Person>();

        var results = AutoMapper.Mapper.Map<IDataReader, IList<Person>>(table.CreateDataReader());
    }
}

这篇关于AutoMapper将IEnumerable映射到DataReader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆