Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Projecting Enum to Enum in another assembly throws an exception #583

Closed
wants to merge 1 commit into from

Conversation

nholling
Copy link

@nholling nholling commented Sep 3, 2014

No description provided.

@nholling
Copy link
Author

nholling commented Sep 3, 2014

Projecting an enum to an enum works, however if you project an enum from one assembly to an enum in another assembly an exception is thrown:

AutoMapper.AutoMapperMappingExceptionUnable to create a map expression from AutoMapper.UnitTests.Projection.ProjectEnumTest+CustomerType to AutoMapper.UnitTests.AnotherAssembly.CustomerType
Unable to create a map expression from AutoMapper.UnitTests.Projection.ProjectEnumTest+CustomerType to AutoMapper.UnitTests.AnotherAssembly.CustomerType
   at AutoMapper.QueryableExtensions.Extensions.CreateMemberBindings(IMappingEngine mappingEngine, ExpressionRequest request, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount) in QueryableExtensions.cs: line 170
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, ExpressionRequest request, Expression instanceParameter, IDictionary`2 typePairCount) in QueryableExtensions.cs: line 90
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, ExpressionRequest request, IDictionary`2 typePairCount) in QueryableExtensions.cs: line 71
   at AutoMapper.QueryableExtensions.Extensions.<>c__DisplayClass1`2.<CreateMapExpression>b__0(ExpressionRequest tp) in QueryableExtensions.cs: line 32
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AutoMapper.Internal.DictionaryFactoryOverride.ConcurrentDictionaryImpl`2.GetOrAdd(TKey key, Func`2 valueFactory) in ConcurrentDictionaryFactory.cs: line 35
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, String[] membersToExpand) in QueryableExtensions.cs: line 30
   at AutoMapper.QueryableExtensions.ProjectionExpression`1.To(String[] membersToExpand) in QueryableExtensions.cs: line 605
   at AutoMapper.QueryableExtensions.ProjectionExpression`1.To() in QueryableExtensions.cs: line 600
   at AutoMapper.UnitTests.Projection.ProjectEnumTest.ProjectingEnumToEnumInAnotherAssembly() in ProjectEnumTest.cs: line 48

@jbogard
Copy link
Member

jbogard commented Sep 12, 2014

Well that's odd, thanks for the failing test!

@jbogard jbogard added the Bug label Sep 12, 2014
@jbogard jbogard added this to the 3.3.0 milestone Sep 12, 2014
@jbogard jbogard added Improvement and removed Bug labels Nov 25, 2014
@jbogard
Copy link
Member

jbogard commented Nov 25, 2014

Digging a little deeper, this is a bit harder it turns out. You have to convert enums at the LINQ level. How would you accomplish this without AutoMapper? Can you send me a LINQ Select projection not using AutoMapper?

customers.Select(c => new Customer {
    FirstName = c.FirstName,
    LastName = c.LastName,
    CustomerType = ????
});

@jbogard jbogard modified the milestones: v.Future, 3.3.0 Nov 25, 2014
@hazzik
Copy link
Contributor

hazzik commented Dec 3, 2014

????

Casting from one enum to another would work (by value). But there is a question: does EF support Enums?

So the possible solutions:

  1. Cast by value
customers.Select(c => new Customer {
    FirstName = c.FirstName,
    LastName = c.LastName,
    CustomerType = (CustomerType)c.CustomerType
});
  1. ToString+Enum.Parse - this definitely will not work with EF (probably will work with NHibernate)
customers.Select(c => new Customer {
    FirstName = c.FirstName,
    LastName = c.LastName,
    CustomerType = (CustomerType) Enum.Parse(typeof (CustomerType), c.CusomerType.ToString())
});
  1. Long IIF (Should work both in EF and NHibernate)
customers.Select(c => new Customer {
    FirstName = c.FirstName,
    LastName = c.LastName,
    CustomerType = = c.CustomerType == A1.CustomerType.M1
                ? A2.CustomerType.M1
                : c.CustomerType == CustomerType.M2
                    ? A2.CustomerType.M1
                    : 0
});

@jbogard jbogard removed this from the v.Future milestone Jan 18, 2016
@yyjdelete
Copy link

yyjdelete commented Apr 19, 2016

Also failed ProjectTo with when models contains Enum -> byte
byte is the underType of the enum, public enum ModelStatus : byte
using ProjectUsing for all models is an workaround, but it's no difference with just using select(x=>new XView{}), is there other way to do this?
Seem I can use ProjectUsing to map just the Enmu

@lbargaoanu
Copy link
Member

@yyjdelete Try the MyGet build.

@lbargaoanu
Copy link
Member

I don't think this is worth fixing. One can work around it by creating a map for the enums and casting in ProjectUsing (or whatever works).

@yyjdelete
Copy link

yyjdelete commented Apr 20, 2016

@lbargaoanu
Yes, you are right. But it's strange when using v5.0.0-ci-1049, the same Exception(maybe) is thrown without ProjectUsing, but an NullReferenceException is thrown when access ex.Message, seems another issue?

System.NullReferenceException: (locale message...)
   at AutoMapper.AutoMapperMappingException.get_Message()

Maybe fixed in new ci, I will check later

@yyjdelete
Copy link

AutoMapperMappingException.Message is work well again in v5.0.0-ci-1052

@lbargaoanu
Copy link
Member

@yyjdelete Yes, I saw that and fixed it yesterday.

@jbogard jbogard closed this May 19, 2016
@lock
Copy link

lock bot commented May 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants