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

AutoMapper 4.1.0 Exception with Proxy on Inherited object with Entity Framework #945

Closed
swilkos opened this issue Oct 22, 2015 · 18 comments
Closed
Labels
Milestone

Comments

@swilkos
Copy link

swilkos commented Oct 22, 2015

Seem to still have an issue with the latest build. When creating an Entity Framework query and attempting to map the object over to a derived class causes a base object mapping exception. Please let me know if more information is needed.

Classes:

    public class ExampleContext : DbContext
    {
        public DbSet<TrainingCourse> TrainingCourses { get; set; }
        public DbSet<TrainingContent> TrainingContents { get; set; }
    }

    public class TrainingCourse
    {
        public TrainingCourse()
        {
            Content = new List<TrainingContent>();
        }

        public TrainingCourse(TrainingCourse entity)
        {

            Mapper.Map(entity, this);
        }

        [Key]
        public int CourseId { get; set; }

        public string CourseName { get; set; }

        public virtual ICollection<TrainingContent> Content { get; set; }
    }

    public class TrainingContent
    {
        public TrainingContent()
        {
        }

        [Key]
        public int ContentId { get; set; }

        public string ContentName { get; set; }

        public virtual TrainingCourse Course { get; set; }

        public int CourseId { get; set; }

    }

    public class TrainingCourseDto
    {
        public int CourseId { get; set; }

        public string CourseName { get; set; }

        public ICollection<TrainingContentDto> Content { get; set; }
    }

    public class ParentTrainingCourseDto : TrainingCourseDto
    {
        [IgnoreMap]
        public new ICollection<TrainingContentDto> Content { get; set; }
    }

    public class TrainingContentDto
    {
        public int ContentId { get; set; }

        public string ContentName { get; set; }

        public ParentTrainingCourseDto Course { get; set; }

        public int CourseId { get; set; }
    }

Initializer:

            Mapper.Initialize(
                cfg =>
                    {
                        cfg.CreateMap<TrainingCourse, TrainingCourseDto>()
                        .Include<TrainingCourse, ParentTrainingCourseDto>();
                        cfg.CreateMap<TrainingCourse, ParentTrainingCourseDto>();
                        cfg.CreateMap<TrainingContent, TrainingContentDto>();


                    });

            Mapper.Configuration.Seal();
            Mapper.AssertConfigurationIsValid();

Code that throws an excption with Automapper 4.1.0

            ExampleContext context = new ExampleContext();
            var course = context.TrainingCourses.FirstOrDefault(n => n.CourseName == "Course 1");
            var dto = Mapper.Map<TrainingCourseDto>(course);

Error thrown:

AutoMapper.AutoMapperMappingException : 

Mapping types:
TrainingCourse -> TrainingCourseDto
AutoMapper.BugExample.TrainingCourse -> AutoMapper.BugExample.TrainingCourseDto

Destination path:
TrainingCourseDto.Content.Content.Content0[0].Course0[0].Course0[0]

Source value:
System.Data.Entity.DynamicProxies.TrainingCourse_1F4098BB48C8E550C9BD06AA4E5078C25E68A8F1D567EB1E395EAFE4C70847D6
  ----> System.InvalidCastException : Unable to cast object of type 'AutoMapper.BugExample.TrainingCourseDto' to type 'AutoMapper.BugExample.ParentTrainingCourseDto'.
@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

Hmmm yeah, can you post what the source object types look like, in their full hierarchy? It looks like AutoMapper is going down the line here.

@lbargaoanu
Copy link
Member

Same as #917.
LinqPad example

@swilkos
Copy link
Author

swilkos commented Oct 23, 2015

The example @lbargaoanu did is spot on. The context of the "ParentTrainingCourseDto" is a way to limit through Automapper how much is filled in and prevent recursion from happening. We've gone a different route for the moment, but it seemed like a straight forward way to use the AutoMapper configuration vs. having to write a bunch of hand coded projections.

@lbargaoanu
Copy link
Member

#917 still fails.

@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

So these are different issues then.

@lbargaoanu
Copy link
Member

Well, they have the same root cause. Returning base destination types when we shouldn't. My fix may be arbitrary but it fixes both.

@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

What fix?

@lbargaoanu
Copy link
Member

#923

@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

Jeez that took like 2 seconds for me to find it sigh. Marking for 4.1.1, I want to get this one out ASAP

@jbogard jbogard added this to the 4.1.1 milestone Oct 23, 2015
@jbogard jbogard added the Bug label Oct 23, 2015
@lbargaoanu
Copy link
Member

Of course I replayed your last check in, but I don't know :) It feels like the source type should be the first. Not exactly an argument, I know.

@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

No that was just a guess of a fix, I didn't really like it. It makes some assumptions on the order of a SelectMany. If yours works I'll just revert my """"fix""""

@jbogard
Copy link
Member

jbogard commented Oct 23, 2015

@swilkos can you check the MyGet release? Just pushed a fix for this (and included integration tests to make sure this stays fixed).

@swilkos
Copy link
Author

swilkos commented Oct 26, 2015

Will do and get back with you.

@swilkos
Copy link
Author

swilkos commented Oct 26, 2015

My apologies, am new to working with MyGet. Using the URL https://www.myget.org/F/automapperdev/api/v2, Get-Package -ListAvailable shows now packages. Is there a different URL to work with?

Thanks!

@jbogard
Copy link
Member

jbogard commented Oct 26, 2015

That's the right URL, are you showing pre-release packages?

@swilkos
Copy link
Author

swilkos commented Oct 26, 2015

Imagine that! Seems to be working as I'd expect it on my sample project. Will plug the pre-release into our main application later today and test it on a larger scale then the small example project I put together when tracking down what was happening.

jarroda added a commit to 2020IP/TwentyTwenty.IdentityServer3.EntityFramework7 that referenced this issue Dec 15, 2015
@Jerome2606
Copy link

Is this problem is fix for you ?

I have the same problem on my project with AutoMapper 4.1.1

@lock
Copy link

lock bot commented May 7, 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 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants