Giter Site home page Giter Site logo

ExecutionEngineException thrown when mapping with IDataReader to DTO with a different datatype for a property about automapper.data HOT 5 CLOSED

automapper avatar automapper commented on September 12, 2024
ExecutionEngineException thrown when mapping with IDataReader to DTO with a different datatype for a property

from automapper.data.

Comments (5)

jumas45 avatar jumas45 commented on September 12, 2024 1

@jbogard Issue still exists. Is this going to be fixed in version 4.1?

from automapper.data.

jbogard avatar jbogard commented on September 12, 2024

From @rlaveycal on February 14, 2014 12:36

The problem still exists in version 3.1.1.

It seems to be caused by the IL generated by DataReaderMapper.CreateBuilder(). This enumerates all the columns of the data reader and assigns the value to the corresponding property on the destination type, with no type checking. In cases where the types don't match there should be an override defined as part of the map definition by calling ForMember() so there is no need to attempt the assignment. My solution below uses this assumption to short circuit the IL generation

First add a parameter to CreateBuilder and build a map of the properties that we don't need to assign

        private static Build CreateBuilder(Type destinationType, IDataRecord dataRecord, IEnumerable<PropertyMap> propertyMaps)
        {
            Build builder;
            BuilderKey builderKey = new BuilderKey(destinationType, dataRecord);
            if (_builderCache.TryGetValue(builderKey, out builder))
            {
                return builder;
            }

            HashSet<string> overriddenProperties = new HashSet<string>();
            foreach (var map in propertyMaps)
            {
                overriddenProperties.Add(map.DestinationProperty.Name);
            }

Then, check each property in the loop - we can share GetName(i) via a variable.

            for (var i = 0; i < dataRecord.FieldCount; i++)
            {
                string name = dataRecord.GetName(i);
                if(overriddenProperties.Contains(name))
                    continue;

Finally the calls to CreateBuilder() need to have the extra parameter supplied - context.GetContextTypeMap().GetPropertyMaps()

from automapper.data.

jbogard avatar jbogard commented on September 12, 2024

Cool, I'll get this in 3.2

On Fri, Feb 14, 2014 at 6:36 AM, rlaveycal [email protected] wrote:

The problem still exists in version 3.1.1.

It seems to be caused by the IL generated by
DataReaderMapper.CreateBuilder(). This enumerates all the columns of the
data reader and assigns the value to the corresponding property on the
destination type, with no type checking. In cases where the types don't
match there should be an override defined as part of the map definition by
calling ForMember() so there is no need to attempt the assignment. My
solution below uses this assumption to short circuit the IL generation

First add a parameter to CreateBuilder and build a map of the properties
that we don't need to assign

    private static Build CreateBuilder(Type destinationType, IDataRecord dataRecord, IEnumerable<PropertyMap> propertyMaps)
    {
        Build builder;
        BuilderKey builderKey = new BuilderKey(destinationType, dataRecord);
        if (_builderCache.TryGetValue(builderKey, out builder))
        {
            return builder;
        }

        HashSet<string> overriddenProperties = new HashSet<string>();
        foreach (var map in propertyMaps)
        {
            overriddenProperties.Add(map.DestinationProperty.Name);
        }

Then, check each property in the loop - we can share GetName(i) via a
variable.

        for (var i = 0; i < dataRecord.FieldCount; i++)
        {
            string name = dataRecord.GetName(i);
            if(overriddenProperties.Contains(name))
                continue;

Finally the calls to CreateBuilder() need to have the extra parameter
supplied - context.GetContextTypeMap().GetPropertyMaps()

Reply to this email directly or view it on GitHubhttps://github.com/AutoMapper/AutoMapper/issues/273#issuecomment-35079680
.

from automapper.data.

jbogard avatar jbogard commented on September 12, 2024

From @rlaveycal on April 24, 2014 13:40

Hi,

This didn't make 3.2...maybe 3.3?

Thanks

from automapper.data.

jumas45 avatar jumas45 commented on September 12, 2024

Temporary Workaround: In my SQL whenever there is a datatype of int, long, decimal,float, double, I am casting it as a string and then I am creating an explicit .ForMember mapping for those columns. It's painful but works.

from automapper.data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.